ActionSheet on iPad not showing properly SwiftUI
Asked Answered
A

1

10

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 ?

Aguish answered 16/12, 2020 at 13:29 Comment(0)
W
14

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()
    }
}
Wrestle answered 16/12, 2020 at 13:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.