Presentation Styles

When you present a standard view controller, it slides up to cover the window. This is the default presentation style. The presentation style determines the appearance of the view controller as well as how the user can interact with it. Here are some of the more common presentation styles that you will encounter:

.automatic

Presents the view controller using a style chosen by the system. Typically this results in a .formSheet presentation. This is the default presentation style.

.formSheet

Presents the view controller centered on top of the existing content.

.fullScreen

Presents the view controller over the entire application.

.overFullScreen

Similar to .fullScreen except the view underneath the presented view controller stays visible. Use this style if the presented view controller has transparency, so that the user can see the view controller underneath.

.popover

Presents the view controller in a popover view on iPad. (On iPhone, using this style falls back to a form sheet presentation style due to space constraints.)

Many of the presentation styles adapt their appearance based on the size of the window. Specifically, they adapt based on the horizontal and vertical size classes, which you will learn about in Chapter 16. Figure 14.10 shows a view controller presented using a popover presentation style on devices of different sizes.

Figure 14.10  Popover adapting to different window sizes

Popover adapting to different window sizes

Action sheets should be presented using the popover style. As you can see in Figure 14.10, on iPad this produces a popover interface with a “pointer” connecting it to the element that triggered it. On iPhone, because of the smaller window size, .popover falls back to .automatic and allows the system to choose the best style.

This is what you want for your alert controller. On iPad, you want it to appear in a popover pointing at the camera bar button. On iPhone, you want the system to select the best style for the screen size (which will be the .formSheet style you just saw in action). Update choosePhotoSource(_:) to tell the alert controller to use the popover presentation style.

Listing 14.4  Setting the modal presentation style (DetailViewController.swift)

@IBAction func choosePhotoSource(_ sender: UIBarButtonItem) {
    let alertController = UIAlertController(title: nil,
                                            message: nil,
                                            preferredStyle: .actionSheet)

    alertController.modalPresentationStyle = .popover

To indicate where the popover should point, you can specify a frame or a bar button item for it to point to. Since you already have a bar button item, that is the better choice here.

In choosePhotoSource(_:), specify the bar button item that the popover should point at.

Listing 14.5  Indicating where the popover should point (DetailViewController.swift)

@IBAction func choosePhotoSource(_ sender: UIBarButtonItem) {
    let alertController = UIAlertController(title: nil,
                                            message: nil,
                                            preferredStyle: .actionSheet)

    alertController.modalPresentationStyle = .popover
    alertController.popoverPresentationController?.barButtonItem = sender

Every view controller has a popoverPresentationController, which is an instance of UIPopoverPresentationController. The popover presentation controller is responsible for managing the appearance of the popover. One of its properties is barButtonItem, which tells the popover to point at the provided bar button item. Alternatively, you can specify a sourceView and a sourceRect if the popover is not presented from a bar button item.

Open the active scheme pop-up and choose an iPad simulator. Build and run the application, navigate to an item’s details, and tap the camera button. The action sheet is presented in a popover pointing at the camera button (Figure 14.11). Notice that there is no cancel action; when an action sheet is presented in a popover, the cancel action is triggered by tapping outside of the popover.

Figure 14.11  iPad presenting the alert controller

iPad presenting the alert controller
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset