UIActionSheet from Popover with iOS8 GM

23,371

Solution 1

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)

Solution 2

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];

Solution 3

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

Solution 4

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.

Solution 5

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)
Share:
23,371

Related videos on Youtube

Tomer Peled
Author by

Tomer Peled

_

Updated on January 05, 2020

Comments

  • Tomer Peled
    Tomer Peled over 4 years

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

    • Jageen
      Jageen almost 10 years
      You mean UIAcitonsheet is deprecated but you still want to use it ???
    • Tomer Peled
      Tomer Peled almost 10 years
      It is deprecated but should still work...
  • Misha
    Misha over 8 years
    what is shareMenu ? is it alertVc?
  • Robert Altman
    Robert Altman over 8 years
    @Misha, Yes. This should be the alertVC