UIActionSheet from Popover with iOS8 GM
Anyone is getting this message while trying to show UIActionSheet from popover?
Your application has presented a UIAlertController () of style UIAlertControllerStyleActionSheet. 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.
Previously to the GM I used some workaround for converting the UIActionSheet to UIAlertController and this is working fine. However it seems that Apple tried to solve the UIActionSheet issues and I didn't want to use my workaround - but it seems that I have no choice...
To support iPad, include this code:
alertView.popoverPresentationController?.sourceView = self.view
alertView.popoverPresentationController?.sourceRect = self.view.bounds
// this is the center of the screen currently but it can be any point in the view
self.presentViewController(alertView, animated: true, completion: nil)
If you are presenting the action sheet after the user makes a selection on a cell within a UITableView
. I found that this works decently well:
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Directions"
message:@"Select mode of transportation:"
preferredStyle:UIAlertControllerStyleActionSheet];
alert.popoverPresentationController.sourceView = cell;
alert.popoverPresentationController.sourceRect = cell.bounds;
UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];
//...
[self presentViewController:alert animated:YES completion:nil];
You need to provide popoverPresentationController
for iPad support. In this, you either specify barButtonItem
or sourceView
. This another thread may help you: Swift UIAlertController - ActionSheet iPad iOS8 Crashes
Actually it is something buggy (I believe) in Xcode for iPhone and iPad designs for now.
- In iPhone same code works perfect and you can see the alert message at same position (always). But for iPad you need to define the alert box's position with
alert.popoverPresentationController.sourceView = self.view; alert.popoverPresentationController.sourceRect = CGRectMake(self.view.bounds.size.width / 2.0 - 105, self.view.bounds.size.height / 2.0 + 70, 1.0, 1.0);
105 and 70 are the approximate dimension differences for iPad portrait design due to different anchor point. - In iPhone design
UIAlertController
comes with 'Modal View' but unfortunately if you use same code for iPad it will not be a 'Modal View'. Which means that you need to write extra code for disabling touches in iPad design. I think it is weird. - In iPad design you need to consider that anchor point is different. It is the bubble triangle point, not the upper left of AlertView.
These are the weird things I see. I think that there must be a standard and if someone wants to go out standards, fine, there can be other options.