Actionsheet Not Working iPad

Action Sheet is not working in ipad ios 13.6

I have tried your code on iPhone(Device:- iPhone 11 Pro 13.6) and iPad(iPad pro (12.9- 4th generation) both and it works. But If you said, I have changed some popover frame, Use the following code:-

   let alert = UIAlertController(title: "Select Image", message: nil, preferredStyle:
UIAlertController.Style.actionSheet)
alert.addAction(UIAlertAction(title: "Camera", style: UIAlertAction.Style.default,
handler: { (res) in
self.btnClickedCamera(tag:2)
}))
alert.addAction(UIAlertAction(title: "Gallery", style: UIAlertAction.Style.default,
handler: { (res) in
self.btnClickedGallery(tag:2)
}))
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (res) in

}))
if let popoverPresentationController = alert.popoverPresentationController {

popoverPresentationController.sourceRect = sender.frame
popoverPresentationController.sourceView = self.view

}
self.present(alert, animated: true, completion: nil)

}

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

demo

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

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

iPad Actionsheet not showing

Use PopOver Controller to display action sheet on ipad. To use actionsheet on Popover controller refer the following links,

UIDatePicker in UIActionSheet on iPad

UIActionSheet on iPad frame too small

Swift UIAlertController - ActionSheet iPad iOS8 Crashes

The error message is telling you that you need to give the alert controller's popoverPresentationController a location so that it can position itself properly. This is easy to do -- just check to see if there's a popover controller and add the sender as the source.

If your button is a UIBarButtonItem:

if let popoverController = alertController.popoverPresentationController {
popoverController.barButtonItem = sender
}
self.presentViewController(alertController, animated: true, completion: nil)

Otherwise:

if let popoverController = alertController.popoverPresentationController {
popoverController.sourceView = sender
popoverController.sourceRect = sender.bounds
}
self.presentViewController(alertController, animated: true, completion: nil)


Related Topics



Leave a reply



Submit