UIActionSheet from Popover with iOS8 GM
Asked Answered
C

6

47

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

Comrade answered 10/9, 2014 at 7:42 Comment(2)
You mean UIAcitonsheet is deprecated but you still want to use it ???Anana
It is deprecated but should still work...Comrade
C
106

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)
Conciliatory answered 12/12, 2014 at 9:52 Comment(2)
what is shareMenu ? is it alertVc?Furnace
@Misha, Yes. This should be the alertVCStephens
S
11

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];
Stasny answered 6/12, 2015 at 17:54 Comment(0)
G
7

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

Gobetween answered 25/9, 2014 at 21:31 Comment(0)
U
2

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.

Understanding answered 24/12, 2015 at 15:57 Comment(0)
M
1

UIAlertController being iOS8 only, and needing to support iOS7, I am using it. I ran into this on a Master view in a Master/Detail layout on iPad. I was able to work around it (not exactly fix it) by raising the UIActionSheet from the parent UISplitViewController using [actionSheet showInView:]. Good luck.

Myke answered 10/9, 2014 at 16:23 Comment(0)
B
1

If you want to present it in the centre with no arrows on iPads and normally on iPhones: [Swift 3+]:

if let popoverController = alertController.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(alertController, animated: true, completion: nil)
Boarder answered 4/1, 2019 at 10:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.