Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActionSheet on iPad not showing properly SwiftUI

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...

actionSheetIpad

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 ?

like image 959
Clément Tengip Avatar asked Dec 16 '20 13:12

Clément Tengip


People also ask

How do I view actionSheet on iPad?

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.

How to show actionSheet in SwiftUI?

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.


1 Answers

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()
    }
}
like image 76
lorem ipsum Avatar answered Oct 16 '22 19:10

lorem ipsum