Present ActionSheet in SwiftUI on iPad
Finally, as tested in iOS 13.4 this has been resolved, at least in the beta. The conflicting constraints warning persists, but the crash is gone. This is now the appropriate way to present an action sheet.
import SwiftUI
struct ContentView : View {
@State var showSheet = false
var body: some View {
VStack {
Button(action: {
self.showSheet.toggle()
}) {
Text("Show")
}
.actionSheet(isPresented: $showSheet, content: { ActionSheet(title: Text("Hello"))
})
}
}
}
struct ContentView_Previews : PreviewProvider {
static var previews: some View {
ContentView()
}
}
ActionSheet on iPad not showing properly SwiftUI
Attach it to something in your View
like the Button
that makes it show up.
struct ASSample: View {
@State var shouldPresentActionScheet1: Bool = false
@State var shouldPresentActionScheet2: Bool = false
var body: some View {
VStack{
Button("show-sheet1", action: {
self.shouldPresentActionScheet1.toggle()
})
.actionSheet(isPresented: $shouldPresentActionScheet1) { () -> ActionSheet in
ActionSheet(title: Text("Ajouter une photo"), buttons: [ActionSheet.Button.default(Text("Prendre une photo"), action: {
//self.shouldPresentImagePicker = true
//self.shouldPresentCamera = true
}), ActionSheet.Button.default(Text("Importer depuis mes photos"), action: {
//self.shouldPresentImagePicker = true
//self.shouldPresentCamera = false
}), ActionSheet.Button.cancel()])
}
Spacer()
Button("show-sheet2", action: {
self.shouldPresentActionScheet2.toggle()
})
.actionSheet(isPresented: $shouldPresentActionScheet2) { () -> ActionSheet in
ActionSheet(title: Text("Ajouter une photo"), buttons: [ActionSheet.Button.default(Text("Prendre une photo"), action: {
//self.shouldPresentImagePicker = true
//self.shouldPresentCamera = true
}), ActionSheet.Button.default(Text("Importer depuis mes photos"), action: {
//self.shouldPresentImagePicker = true
//self.shouldPresentCamera = false
}), ActionSheet.Button.cancel()])
}
}
}
}
struct ASSample_Previews: PreviewProvider {
static var previews: some View {
ASSample()
}
}
Unable to present ActionSheet via a NavigationBarItem in SwiftUI on an iPad
Yes, it is a bug, but probably different - that Apple does not allow to change anchor and direction of shown ActionSheet
, because it is shown, but always to the right of originated control on iPad. To prove this it is enough to change location of button in Navigation
Here is example of placing at .leading
position. Tested with Xcode 12 / iOS 14
.navigationBarItems(leading:
Button(action: {
// Works on iPhone, fails on iPad
self.isNavButtonSheetPresented.toggle()
}) {
Text("Show Nav")
}
.actionSheet(isPresented: $isNavButtonSheetPresented,
content: {
ActionSheet(title: Text("ActionSheet"))
})
)
Note: SwiftUI 2.0 .toolbar
behaves in the same way, ie. has same bug.
Conditionally present ActionSheet SwiftUI
.sheet
is an instance method VStack
, so you can't do what you did - it's not a legal Swift syntax.
The simplest approach is to have the condition over the VStack
view:
if(generalConstants.shouldShowUpdateSheet) {
VStack {
Text(" ")
}
.sheet(isPresented: $isShowingAppStoreUpdateNotification) {
UpdatesView()
}
} else {
VStack {
Text(" ")
}
}
but, of course, this isn't very DRY.
Instead, keep the logic of how the view behaves in the view model / state, and let the View just react to data changes. What I mean is, only set isShowingAppStoreUpdateNotification
to true
when all the conditions that you want are satisfied, and keep the view as-is
@State var isShowingAppStoreUpdateNotification = generalConstants.shouldShowUpdateSheet
var body: some View {
VStack {
Text(" ")
}
.sheet(isPresented: $isShowingAppStoreUpdateNotification) {
UpdatesView()
}
}
How to present actionSheet in iPhone style on iPad on iOS13?
The desired behaviour is not possible since iOS 13.2 anymore.
Related Topics
Conflicting Definition of Swift Struct and Array
Accessing a String Enum by Index
Avoid Consecutive "If Let" Declarations in Swift
Swift Calling Static Methods: Type(Of: Self) VS Explicit Class Name
How to Get the Download Progress with the New Try Await Urlsession.Shared.Download(...)
How to Pass an Error Pointer in the Swift Language
Swift: How to Animate the Rowheight of a Uitableview
Kvo with Shared Nsuserdefaults in Swift
An Nsmanagedobject of Class 'Classname' Must Have a Valid Nsentitydescription.' Error
Swiftui: How Do Style Text View with Different Font and Colour on String Subranges
Scenekit -- How to Get Animations for a .Dae Model
How to Color/Customize the Uiimagepickercontroller's Navigation Bar
Button Border with Transparent Background in Swift
Swift Check Type Against a Generic Type
Collection View Cell Button Not Triggering Action
Get an Error When Trying to Get All the Photos from Phassetcollection.Fetchassetcollections