Senger CodeLab 🚀

Presenting a UIAlertController properly on an iPad using iOS 8

September 29, 2026

Presenting a UIAlertController properly on an iPad using iOS 8

Effectively presenting a UIAlertController on an iPad using iOS 8 required developers to navigate some specific nuances related to popover presentation. Unlike iPhones, iPads rely on popovers to display alert controllers, necessitating the specification of an anchor point. Ignoring this crucial step would result in a crash, leaving users with a frustrating experience. This guide will walk you through the correct method of presenting your alert controllers on iPads running iOS 8, ensuring a smooth and user-friendly application experience. We’ll explore best practices, common pitfalls, and provide clear, actionable code examples to help you master this essential aspect of iOS development. Presenting a UIAlertController properly avoids unexpected crashes and provides a better user experience.

Understanding the Challenge: UIAlertController on iPad

The primary difference between presenting a UIAlertController on an iPhone versus an iPad lies in the presentation style. On iPhones, the alert controller is typically presented modally, taking up the entire screen. iPads, however, utilize popovers. Popovers require an anchor point, indicating where the popover should originate from. This anchor point is typically a UIBarButtonItem or a UIView within your interface. Failing to specify this anchor point on an iPad running iOS 8 will lead to a runtime exception, specifically a NSInvalidArgumentException with a message indicating that the popover presentation controller requires a source view and source rect.

This requirement stems from the iPad’s larger screen size and the need to present alerts in a non-obtrusive manner. Popovers allow users to continue interacting with the underlying content while the alert is displayed. Understanding this fundamental difference is key to ensuring your app behaves consistently across different iOS devices. The anchor point tells the system where the alert originated from, providing visual context for the user. Without it, the system doesn’t know where to position the popover, leading to the crash.

Apple’s documentation emphasizes the importance of handling popover presentations correctly. According to Apple’s Human Interface Guidelines, “Popovers are best suited for presenting a discrete set of options or information related to a specific control or area of the screen” [^1^]. This reinforces the need for a clear visual association between the alert and its source. Developers should always test their UIAlertController implementations on both iPhone and iPad to catch any device-specific issues early in the development process. Proper testing ensures a consistent user experience.

Implementing the Correct Presentation Method

To correctly present a UIAlertController on an iPad using iOS 8, you must configure the popoverPresentationController property of the alert controller. This property is only available when the alert controller’s modalPresentationStyle is set to UIModalPresentationPopover. This ensures that the system knows you intend to present the alert as a popover. Once the presentation style is set, you need to specify the sourceView and sourceRect properties of the popoverPresentationController.

The sourceView property should be set to the view that will act as the anchor for the popover. This is typically the button or other UI element that triggered the alert. The sourceRect property defines the rectangle within the sourceView that the popover should originate from. Setting this to the bounds of the sourceView is a common practice. Here’s a code snippet illustrating the correct implementation:

let alertController = UIAlertController(title: "Alert Title", message: "Alert Message", preferredStyle: .actionSheet) //Use .alert for simple alerts alertController.modalPresentationStyle = .popover if let popoverController = alertController.popoverPresentationController { popoverController.sourceView = self.view // Replace self.view with your button or UI element popoverController.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0) //Positioning the alert popoverController.permittedArrowDirections = [] //Hiding the arrow } self.present(alertController, animated: true, completion: nil) 

This code snippet first creates the UIAlertController and sets its modalPresentationStyle to .popover. It then accesses the popoverPresentationController and sets its sourceView to the current view (replace self.view with your actual source view). Finally, it sets the sourceRect to the bounds of the source view. This ensures that the popover is anchored to the specified view. Remember to add actions to the alert controller before presenting it. Always test this code thoroughly on an iPad device or simulator.

Handling Different Alert Styles and Scenarios

While the basic implementation remains the same, you might encounter different scenarios depending on the type of alert you’re presenting. For example, action sheets (UIAlertController with preferredStyle set to .actionSheet) are often presented from a UIBarButtonItem in a navigation bar. In this case, you would set the barButtonItem property of the popoverPresentationController instead of the sourceView and sourceRect. Similarly, presenting an alert from a table view cell might require adjusting the sourceRect to align with the cell’s content.

Here’s how to present an action sheet from a UIBarButtonItem:

let alertController = UIAlertController(title: "Options", message: nil, preferredStyle: .actionSheet) alertController.modalPresentationStyle = .popover if let popoverController = alertController.popoverPresentationController { popoverController.barButtonItem = self.navigationItem.rightBarButtonItem // Replace with your UIBarButtonItem } self.present(alertController, animated: true, completion: nil) 

In this example, the barButtonItem property is set to the rightBarButtonItem of the navigation item. This tells the system to anchor the popover to the button in the navigation bar. Remember to handle cases where the barButtonItem might be nil. It’s also important to consider the context in which the alert is being presented. For instance, if you’re presenting an alert in response to a user tap on a specific element, ensure that the alert’s message and options are relevant to that element. Careful consideration of these factors will lead to a more intuitive and user-friendly experience. Proper UI design is key.

Infographic illustrating UIAlertController presentation on iPad vs. iPhone
Troubleshooting Common Issues -----------------------------

Despite following the correct implementation steps, you might still encounter issues when presenting UIAlertController on iPads. One common problem is the “attempt to present <uialertcontroller: …=”"> on <uiviewcontroller: …=""> whose view is not in the window hierarchy" error. This typically occurs when you’re trying to present the alert controller before the view controller’s view has been added to the window. Ensure that you’re presenting the alert controller within the viewDidAppear(_:) method or after the view has been fully loaded.</uiviewcontroller:></uialertcontroller:>

Another potential issue is the popover not appearing in the correct location. This could be due to an incorrect sourceRect or sourceView. Double-check that these properties are set correctly and that they accurately reflect the location of the anchor point. Additionally, be mindful of the device’s orientation. If the orientation changes after the sourceRect has been set, the popover might not appear in the expected location. Consider updating the sourceRect whenever the device orientation changes.

Furthermore, ensure that the UIAlertController is not already being presented. Attempting to present the same alert controller multiple times can lead to unexpected behavior. Implement a mechanism to prevent multiple presentations of the same alert. Thorough testing and debugging are crucial for identifying and resolving these issues. Use the debugger to inspect the values of the sourceView, sourceRect, and other relevant properties. Remember to consult the official Apple documentation and community forums for additional troubleshooting tips [^2^].

  • Always check your sourceView and sourceRect for accuracy.
  • Ensure your view controller’s view is in the window hierarchy before presenting.

Best Practices for UIAlertController Presentation

To ensure a smooth and consistent user experience when presenting UIAlertController on iPads, consider these best practices:

  1. Always specify a source view and source rect: Failing to do so will result in a crash.
  2. Handle different alert styles appropriately: Adjust the presentation logic based on whether you’re presenting an alert or an action sheet.
  3. Test on both iPhone and iPad: Ensure that your alert presentations work correctly on all devices.
  • Use UIAlertController for simple alerts and action sheets.
  • Consider custom view controllers for more complex presentations.

FAQ: UIAlertController on iPad

Q: Why does my app crash when presenting a `UIAlertController` on an iPad?
A: The most common reason is the missing `sourceView` and `sourceRect` properties on the `popoverPresentationController`. iPads require these properties to be set when presenting alerts as popovers.
Q: How do I present an action sheet from a `UIBarButtonItem`?
A: Set the `barButtonItem` property of the `popoverPresentationController` to your `UIBarButtonItem` instance.
Q: Can I customize the appearance of the popover?
A: Yes, you can customize various aspects of the popover, such as its background color, arrow direction, and content size. Refer to the `UIPopoverPresentationController` documentation for more details \[^3^\].
In summary, successfully presenting a `UIAlertController` on an iPad using iOS 8 hinges on correctly configuring the `popoverPresentationController`. By understanding the differences between iPhone and iPad presentations and following the guidelines outlined in this article, you can avoid common pitfalls and ensure a seamless user experience. Remember to always test your code thoroughly on both device types and to consult the official Apple documentation for the most up-to-date information.

Ready to elevate your iOS development skills? Explore the world of adaptive UI design and create apps that shine on every device. Dive into further reading on UIPopoverPresentationController and experiment with different presentation styles. Don’t forget to check out our other articles on iOS best practices, including effective memory management. Your next great app is waiting to be built!

1 Apple. (n.d.). Human Interface Guidelines: Popovers. [Link to Apple’s Human Interface Guidelines for Popovers] (Hypothetical Link)

2 Apple. (n.d.). UIAlertController Class Reference. [Link to Apple’s UIAlertController Class Reference] (Hypothetical Link)

3 Apple. (n.d.). UIPopoverPresentationController Class Reference. [Link to Apple’s UIPopoverPresentationController Class Reference] (Hypothetical Link)

Question & Answer :
With iOS 8.0, Apple introduced UIAlertController to replace UIActionSheet. Unfortunately, Apple didn’t add any information on how to present it. I found an entry about it on hayaGeek’s blog, however, it doesn’t seem to work on iPad. The view is totally misplaced:

Misplaced: Misplaced image

Correct: enter image description here

I use the following code to show it on the interface:

let alert = UIAlertController() // setting buttons self.presentModalViewController(alert, animated: true) 

Is there another way to add it for iPad? Or did Apple just forget the iPad, or not implemented, yet?

You can present a UIAlertController from a popover by using UIPopoverPresentationController.

In Obj-C:

UIViewController *self; // code assumes you're in a view controller UIButton *button; // the button you want to show the popup sheet from UIAlertController *alertController; UIAlertAction *destroyAction; UIAlertAction *otherAction; alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet]; destroyAction = [UIAlertAction actionWithTitle:@"Remove All Data" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) { // do destructive stuff here }]; otherAction = [UIAlertAction actionWithTitle:@"Blah" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { // do something here }]; // note: you can control the order buttons are shown, unlike UIActionSheet [alertController addAction:destroyAction]; [alertController addAction:otherAction]; [alertController setModalPresentationStyle:UIModalPresentationPopover]; UIPopoverPresentationController *popPresenter = [alertController popoverPresentationController]; popPresenter.sourceView = button; popPresenter.sourceRect = button.bounds; [self presentViewController:alertController animated:YES completion:nil]; 

Editing for Swift 4.2, though there are many blogs available for the same but it may save your time to go and search for them.

if let popoverController = yourAlert.popoverPresentationController { popoverController.sourceView = self.view //to set the source of your alert popoverController.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0) // you can set this as per your requirement. popoverController.permittedArrowDirections = [] //to hide the arrow of any particular direction }