An object that manages the display of content in a popover.


From the time a popover is presented until the time it is dismissed, UIKit uses an instance of this class to manage the presentation behavior. You use instances of this class as-is to configure aspects of the popover appearance and behavior for view controllers whose presentation style is set to popover.

In nearly all cases, you use this class as-is and do not create instances of it directly. UIKit creates an instance of this class automatically when you present a view controller using the popover style. You can retrieve that instance from the presented view controller’s popoverPresentationController property and use it to configure the popover behavior.

If you do not want to configure a popover immediately after presenting a view controller, you can use a delegate object to configure the popover instead. During the presentation process, the popover presentation controller calls various methods of its delegate—an object that conforms to the UIPopoverPresentationControllerDelegate protocol—to ask for information and to inform it about the state of the presentation. Your delegate object can use those methods to configure the popover and adjust its behavior as needed. For information about how to implement a delegate for a popover presentation controller, see UIPopoverPresentationControllerDelegate.

Configuring a Popover for Display

To display a popover, set the presentation style of your view controller to popover and call the present(_:animated:completion:) method. Presenting a view controller with the popover style creates a popover presentation controller to manage the presentation process. You can retrieve that presentation controller from the presented view controller’s popoverPresentationController property and use the object to configure the popover behavior.

Listing 1 shows an example of how you present a view controller using the popover style. After setting the style and calling the present(_:animated:completion:) method, you can fetch the presentation controller and modify its properties. When configuring popovers, always specify either a bar button item or a source view and rectangle as the anchor point for the popover. You can configure other properties as well to accommodate the presented content.

Listing 1

Presenting a popover

// Present the view controller using the popover style.
myPopoverViewController.modalPresentationStyle = UIModalPresentationPopover;
[self presentViewController:myPopoverViewController animated: YES completion: nil];
// Get the popover presentation controller and configure it.
UIPopoverPresentationController *presentationController =
         [myPopoverViewController popoverPresentationController];
presentationController.permittedArrowDirections =
         UIPopoverArrowDirectionLeft | UIPopoverArrowDirectionRight;
presentationController.sourceView = myView;
presentationController.sourceRect = sourceRect;

Configuring the popover presentation controller after calling present(_:animated:completion:) might seem counter-intuitive but UIKit does not create a presentation controller until after you initiate a presentation. In addition, UIKit must wait until the next update cycle to display new content onscreen anyway. That delay gives you time to configure the presentation controller for your popover.


Customizing the Popover Behavior

var delegate: UIPopoverPresentationControllerDelegate?

The delegate that handles popover-related messages.

protocol UIPopoverPresentationControllerDelegate

The methods of the UIPopoverPresentationControllerDelegate protocol let you customize the behavior of a popover-based presentation. A popover presentation controller notifies your delegate at appropriate points during the presentation process. You can use the delegate methods to customize this process and respond to changes dynamically.

Configuring the Popover Appearance

var popoverLayoutMargins: UIEdgeInsets

The margins that define the portion of the screen in which it is permissible to display the popover.

var backgroundColor: UIColor?

The color of the popover’s backdrop view.

var passthroughViews: [UIView]?

An array of views that the user can interact with while the popover is visible.

var popoverBackgroundViewClass: UIPopoverBackgroundViewMethods.Type?

The class to use for displaying the popover background content.

var canOverlapSourceViewRect: Bool

A Boolean value indicating whether the popover can overlap its view rectangle.

Specifying the Popover’s Anchor Point

var barButtonItem: UIBarButtonItem?

The bar button item on which to anchor the popover.

var sourceView: UIView?

The view containing the anchor rectangle for the popover.

var sourceRect: CGRect

The rectangle in the specified view in which to anchor the popover.

Configuring the Popover Arrows

var permittedArrowDirections: UIPopoverArrowDirection

The arrow directions that you prefer for the popover.

var arrowDirection: UIPopoverArrowDirection

The arrow direction in use by the popover.


See Also


class UIPopoverBackgroundView

The background appearance for a popover.

protocol UIPopoverBackgroundViewMethods

A set of methods that UIPopoverBackgroundView subclasses must implement.