I\'m using ActionSheet in my application. On my iPhone it works, but it doesn\'t on the iPad simulator.
this is my code:
@IBAction func dialog(send
add statements in the following terms before presented.
optionMenu.popoverPresentationController.sourceView = self.view;
optionMenu.popoverPresentationController.sourceRect =
CGRectMake(0,0,1.0,1.0);
@IBAction func dialog(sender: AnyObject) {
...
optionMenu.popoverPresentationController.sourceView = self.view;
optionMenu.popoverPresentationController.sourceRect = CGRectMake(0,0,1.0,1.0);
self.presentViewController(optionMenu, animated: true, completion: nil)
}
it will work well.
Swift 3
As said before, you should configure UIAlertController to be presented on a specific point on iPAD.
Example for navigation bar:
// 1
let optionMenu = UIAlertController(title: nil, message: "Choose an option", preferredStyle: .actionSheet)
// 2
let deleteAction = UIAlertAction(title: "Option 1", style: .default, handler: {
(alert: UIAlertAction!) -> Void in
print("option 1 pressed")
})
let saveAction = UIAlertAction(title: "Option 2", style: .default, handler: {
(alert: UIAlertAction!) -> Void in
print("option 2 pressed")
})
//
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: {
(alert: UIAlertAction!) -> Void in
print("Cancelled")
})
// 4
optionMenu.addAction(deleteAction)
optionMenu.addAction(saveAction)
optionMenu.addAction(cancelAction)
// 5
optionMenu.popoverPresentationController?.barButtonItem = self.navigationItem.rightBarButtonItem
self.present(optionMenu, animated: true) {
print("option menu presented")
}
you need to add this for Ipad
alertControler.popoverPresentationController?.sourceView = self.view
If you wish to present it in the centre with no arrows [Swift 3+]:
if let popoverController = optionMenu.popoverPresentationController {
popoverController.sourceView = self.view
popoverController.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)
popoverController.permittedArrowDirections = []
}
self.present(optionMenu, animated: true, completion: nil)
Same problem for me. I had a UIAlertController that worked fine on phone, but crashed on iPad. The sheet pops up when a cell is tapped from a table view.
For Swift 3, I added 3 lines of code right before presenting it:
...
sheet.popoverPresentationController?.sourceView = self.view
sheet.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection()
sheet.popoverPresentationController?.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)
self.present(sheet, animated: true, completion: nil)
I've done this package to manage ActionSheet and Popover in iPhone, Ipad and Mac. https://github.com/AndreaMiotto/ActionOver