0

Here is my code

let selectRoute = UIAlertController(title: "Select a Route", message: "Please select the route you want to create your trip on", preferredStyle: .actionSheet)

    let route1 = UIAlertAction(title: "Route 1", style: .default) { (action) in
        self.reminder()
        self.routeID = 1
        self.tableView.reloadData()
    }

    let route2 = UIAlertAction(title: "Route 2", style: .default) { (action) in

    }

    selectRoute.addAction(route1)
    selectRoute.addAction(route2)

    if let popoverPresentationController = selectRoute.popoverPresentationController {
        popoverPresentationController.sourceView = self.view
        popoverPresentationController.sourceRect = self.view.bounds
    }

    self.present(selectRoute, animated: true, completion: nil)

When I run this, the actionSheet just appears just over the Navigation Controller really small, anyone know a fix?

  • How are you presenting the `UIAlertController`? – Upholder Of Truth Jul 14 '18 at 10:29
  • Let me guess: Crash on iPad, no? Did you look for the error? – Larme Jul 14 '18 at 10:35
  • @Larme Yes the thread 1 signal sigabrt error – Bob Henderson Jul 14 '18 at 12:12
  • Are you trying to display an alert or an action sheet because the `UIAlertController` can do both? The alert will not generate the error but the action sheet will because it needs to be anchored to something as the error is indicating. – Upholder Of Truth Jul 14 '18 at 13:34
  • @UpholderOfTruth an action sheet – Bob Henderson Jul 14 '18 at 23:12
  • I thought so. On an iPad an action sheet is displayed as a popover and so must be anchored to some other view i.e. where does its arrow point to. This can be done by either specifying a specific `UIBarButtonItem` or a `UIView` and the `CGRect` within that view. As the error and answers say you can do this using the `barButtonItem` or `sourceView` and `sourceRect` properties of it's associated `UIPopoverPresentationController` before presenting or by doing the same in the `prepareForePopoverPresentaton` method of the view controller presenting it. – Upholder Of Truth Jul 14 '18 at 23:25
  • If possible @UpholderOfTruth could you give some example I'm a little confused and all the other answers don't work for me – Bob Henderson Jul 14 '18 at 23:41
  • The answer provided seams clear enough to me. The only way to make it clearer is for you to post the code your are using to display the `UIAlertController` so that an example can be specific to your usage. – Upholder Of Truth Jul 14 '18 at 23:44
  • @UpholderOfTruth the problem with the answer is i'm not using a tab bar controller – Bob Henderson Jul 14 '18 at 23:52
  • The answer as it is now isn't specific to a tab bar controller it just uses self.view and self.myButton.frame but you can adjust them to what you want. As I said to make it clearer you need to post the code your are using with some explanation. – Upholder Of Truth Jul 15 '18 at 00:00
  • @UpholderOfTruth Ok, i've updated it – Bob Henderson Jul 15 '18 at 00:08
  • That has changed the entire question as it's no longer about crashing. From looking at your code you are anchoring to self.view and the entire bounds of it so the `UIAlertController` is trying to be outside it resulting in your small size. What is the user tapping to actually show the alert controller. Using that as the anchor is probably more appropriate. – Upholder Of Truth Jul 15 '18 at 00:21
  • The alert appears in viewDidLoad – Bob Henderson Jul 15 '18 at 00:56

1 Answers1

0

On iPad the alert will be displayed as a popover using the UIPopoverPresentationController, it requires you define an anchor point for the presentation of the popover using either a sourceView and sourceRect or a barButtonItem.

To support iPad, include this code:

alertController.popoverPresentationController?.sourceView = self.view
alertController.popoverPresentationController?.sourceRect = self.myButton.frame
self.presentViewController(alertController, animated: true, completion: nil)

here is an example: Suppose you have a button and you are going to show alert controller below of the button:

@IBOutlet weak var myBtn: UIButton!

let alertController = UIAlertController(title: "title :)", message:"message...", preferredStyle: .actionSheet)
let defaultAction = UIAlertAction(title: "cancel", style: .cancel, handler: nil)

let deleteAction = UIAlertAction(title: "delete", style: .destructive) { (action: UIAlertAction) in
        //Do something
    }

let addAction = UIAlertAction(title: "...", style: .default) { (action) in
        //do something
    }

alertController.addAction(addAction)
alertController.addAction(deleteAction)
alertController.addAction(defaultAction)

alertController.popoverPresentationController?.sourceRect = self.myBtn.frame
alertController.popoverPresentationController?.sourceView = self.view
self.present(alertController, animated: true, completion: nil)

If you are displaying the action sheet after the user makes a selection on a cell within a UITableView. you can use this code:

alertController.popoverPresentationController.sourceView = cell
alertController.popoverPresentationController.sourceRect = cell.bounds
Hasti Ranjkesh
  • 643
  • 10
  • 26
  • No does not work, and also will this still work on iPhone? – Bob Henderson Jul 14 '18 at 10:59
  • @BobHenderson yes It will work on both iPhone and iPad. – Hasti Ranjkesh Jul 14 '18 at 11:01
  • What is "cell" supposed to be? – Bob Henderson Jul 14 '18 at 11:05
  • @BobHenderson in my example I wanted to show the alert on my tableview cell. Forget about it and just set a sourceRect and a sourceView. something like this: alertController.popoverPresentationController?.sourceView = self.view alertController.popoverPresentationController?.sourceRect = myRect – Hasti Ranjkesh Jul 14 '18 at 11:09
  • what's myRect meant to be? – Bob Henderson Jul 14 '18 at 11:13
  • @BobHenderson I edited the answer, please see it again :) – Hasti Ranjkesh Jul 14 '18 at 11:36
  • I don't understand what you mean by using a barbuttonitem property, could you explain further? where should I connect this from? – Bob Henderson Jul 14 '18 at 12:18
  • I'm not using a tab bar controller so what do I link my barbuttonitem to? – Bob Henderson Jul 14 '18 at 23:40
  • @BobHenderson I saw your code, you need to determine where do you want to present the popover, based on your code (popoverPresentationController.sourceRect = self.view.bounds) It depends on your view because you use the view bounds for presenting popover. Try to set a custom rect like this popoverPresentationController.sourceRect = CGRect(x: 0, y: 0, width: 200, height: 200) then you can find out the difference. – Hasti Ranjkesh Jul 15 '18 at 06:08
  • yeah so I tried that, any way to make it bigger? – Bob Henderson Jul 15 '18 at 10:25
  • @BobHenderson Are you saying the actionSheet size is smaller than the standard size? because It has a standard size and look in apple ecosystem and I don't think you can change it. Are you running on iPhone or iPad? – Hasti Ranjkesh Jul 15 '18 at 10:39
  • well on the ipad it's the same as iphone size but anyway to make it look visually better such as bigger on the ipad? – Bob Henderson Jul 15 '18 at 11:12
  • @BobHenderson I think there is no way to change it, but you can create a custom alert controller, like this https://code.tutsplus.com/tutorials/create-a-custom-alert-controller-in-ios-10-swift-3--cms-27589 – Hasti Ranjkesh Jul 15 '18 at 11:29