I am trying to let my users choose Take a photo or pick from library in an ActionSheet
It works well on Iphone but not on Ipad
On Ipad : ActionSheet is at the top of screen, and not readable...
All questions I read about this problem on StackOverflow, are talking about crash (that's not my case) or are older than SwiftUI
My code :
struct AjoutView: View {
@State private var image : Image?
@State private var shouldPresentImagePicker = false
@State private var shouldPresentActionScheet = false
@State private var shouldPresentCamera = false
var body: some View {
VStack{
...
}
.sheet(isPresented: $shouldPresentImagePicker, onDismiss: loadImage) {
SUImagePickerView(sourceType: self.shouldPresentCamera ? .camera : .photoLibrary, image: self.$image, isPresented: self.$shouldPresentImagePicker, dispId: disp.id)
}
.actionSheet(isPresented: $shouldPresentActionScheet) { () -> 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()])
}
}
}
What is missing in my code ?
On iPad, UIKit requires that you display an action sheet inside a popover. The following image shows an action sheet anchored to a bar button item. To display your action sheet in a popover, specify your popover's anchor point using the popoverPresentationController property of your alert controller.
You show an action sheet by using the actionSheet(isPresented:content:) view modifier to create an action sheet, which then appears whenever the bound isPresented value is true . The content closure you provide to this modifier produces a customized instance of the ActionSheet type.
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()
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With