Action Sheet is a native IOS component to show a multi-option picker in IOS Apps. I have noted that sometimes it works well in iPhones but causes crash in Ipad. In this article, I will show you the best way to show the action sheet in IOS which works for both, iPhone & iPad.

Code to Show Action Sheet in IOS Swift
func showActionSheet(){
let alert:UIAlertController = UIAlertController(title: "Choose Option", message: nil, preferredStyle: UIAlertController.Style.actionSheet)
let cameraAction = UIAlertAction(title: "Camera", style: UIAlertAction.Style.default)
{
UIAlertAction in
// Do your stuff here when action performed
}
let gallaryAction = UIAlertAction(title: "Gallery", style: UIAlertAction.Style.default)
{
UIAlertAction in
// Do your stuff here when action performed
}
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertAction.Style.cancel)
{
UIAlertAction in
// Do your stuff here when cancel pressed
}
alert.addAction(cameraAction)
alert.addAction(gallaryAction)
alert.addAction(cancelAction)
// Set the following properties to avoid crash in Ipad
if let popoverController = alert.popoverPresentationController {
popoverController.sourceView = self.view //to set the source of your alert
popoverController.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.maxY, width: 0, height: 0) // you can set this as per your requirement.
}
self.present(alert, animated: true, completion: nil)
}
Troubleshooting
A common issue which we have to face especially in iPads is a crash with the following logs.
*** Terminating app due to uncaught exception ‘NSGenericException’, reason: ‘Your application has presented a UIAlertController (<UIAlertController: 0x7fd62b00e000>) of style UIAlertControllerStyleActionSheet from UINavigationController (<UINavigationController: 0x7fd62b825e00>). The modalPresentationStyle of a UIAlertController with this style is UIModalPresentationPopover. You must provide location information for this popover through the alert controller’s popoverPresentationController. You must provide either a sourceView and sourceRect or a barButtonItem. If this information is not known when you present the alert controller, you may provide it in the UIPopoverPresentationControllerDelegate method -prepareForPopoverPresentation.’
It’s obvious from the error message that we need to provide location information for the popover. For that, we need to set sourceView
and sourceRect
or BarButtonItem
for the UIAlertController
.
To resolve this error don’t forget to add the following code before calling self.present(...)
method.
// Set the following properties to avoid crash in Ipad
if let popoverController = alert.popoverPresentationController {
popoverController.sourceView = self.view //to set the source of your alert
popoverController.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.maxY, width: 0, height: 0) // you can set this as per your requirement.
}
That’s it. If you are facing any other specific issue, don’t hesitate to ask in the comments section.
For other helping solutions, have a look at our Tutorials List.