简体   繁体   中英

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?

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()
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM