Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 890
Tomer Peled Avatar asked Sep 10 '14 07:09

Tomer Peled


4 Answers

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)
like image 173
Philip Jang Avatar answered Nov 05 '22 20:11

Philip Jang


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];
like image 30
Stunner Avatar answered Nov 05 '22 22:11

Stunner


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

like image 7
Aнгел Avatar answered Nov 05 '22 21:11

Aнгел


Actually it is something buggy (I believe) in Xcode for iPhone and iPad designs for now.

  1. 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.
  2. 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.
  3. 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.

like image 2
icould Avatar answered Nov 05 '22 21:11

icould