Popovers and modal views provide ways to temporarily display some information to users. The information that is displayed is usually contextual and related to an action performed by the user.
Both popovers and modal views interrupt the user's journey through your application; the user must interact with the popover/modal view before using the rest of the application. Popovers are dismissed by tapping outside the bounds of the popover; modal views are dismissed by using a user-defined cancel button located in the modal view. Popovers are only available on iPads, whereas modal views are available on both the iPad and iPhone.
A popover view is one that is revealed when a control is tapped. A popover appears attached to the control that was tapped to reveal it (see Figure 20.1).
Popovers are only supported on the iPad, and should be used to display additional information related to the control that displays it. When presenting a popover, you do not provide a Done or Cancel button; popovers are dismissed when the user taps outside the popover.
To present a scene in your storyboard in a popover, simply create a popover presentation segue from a button in one of the other scenes of your view controller to the scene you wish to use within the popover (see Figure 20.2).
You can use the Attribute inspector to configure the popover presentation segue (see Figure 20.3). The Anchor attribute references a button (or bar button item) in the presenting view controller. The popover will be anchored to this control. By default, popovers are dismissed as soon as the user taps outside them. If you do not want the popover to be dismissed when some controls are tapped, you can set up the Passthrough attribute to reference them.
A modal view can be created in a couple of different ways; the most common is to use a Present Modally segue from a button in a scene to another scene (see Figure 20.4).
The Present Modally segue has a few presentation styles that can be set up using the Attribute inspector:
The default setting is to present the modal view so that it takes up the entire screen. A commonly used presentation style is form sheet. On an iPhone, the form sheet presentation style and the full screen presentation style achieve identical effects. However, on the iPad, the form sheet presentation style causes the modal view to appear as a self-contained form centered in the presenting view (see Figure 20.5).
When a view is presented modally, there is no system-provided means to dismiss it and return back to the presenting view. If you do not provide a way to dismiss the modal view, then your user will be unable to use the rest of your application (see Figure 20.6).
Modal views are used when your app needs to collect some vital information before proceeding. Typically you add a close button in the modal view that, when tapped, will dismiss it. You cannot use a segue to dismiss a modal view; instead you must call the dismissViewControllerAnimated
method on the modal view controller. This method allows you to provide an optional completion handler that is executed when the modal view is dismissed.
@IBAction func onDismissModalView(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion: nil);
}
If you do not wish to use a segue, you can alternately present a scene in your storyboard modally by using the following code snippet attached to a UIButton
instance in the presenting view controller:
@IBAction func onPresentModalView(sender: AnyObject) {
let modalViewController:ModalViewController =
(self.storyboard!.instantiateViewControllerWithIdentifier
("ModalViewController") as? ModalViewController)!
modalViewController.modalPresentationStyle =
UIModalPresentationStyle.FormSheet
self.presentViewController(modalViewController,
animated: true, completion: nil)
}
In this Try It, you create a simple iPad-only application based on the Single View Application template called PopoverTest, which displays an image and some information on the image in a popover.
PopoverTest
.
Assets.xcassets
file by clicking on it in the project navigator.Images
folder in this chapter's download from the book website.Sunflower
.Sunflower_1x.jpg
, sunflower_2x.jpg
, and Sunflower_3x.jpg
images from this chapter's resources into the appropriate placeholders in the image set.UIImageView
instance to the default scene.
Image View
to the default scene of the storyboard. Place it above the tab bar.imageView
.var image:UIImage!
viewDidLoad
method to load an image and set up the image view:image = UIImage(named: "Sunflower")
imageView.image = image
imageView.contentMode = UIViewContentMode.ScaleAspectFit
prepareForSegue(segue: sender:)
to the view controller class.override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "imageInformationSegue" {
let viewController:ImageInformationViewController =
segue.destinationViewController as!
ImageInformationViewController
viewController.imageBeingDisplayed = self.image
}
}
ImageInformationViewController
by selecting File New. Ensure the class is a subclass of UIViewController
(see Figure 20.7).
ImageInformationViewController
.Table 20.1 Layout Constraints
Element | Left | Top | Right | Width | Height |
Height (label) | 16 | 20 | 115 | 21 | |
Height (text field) | 24 | 0 | 16 | 30 | |
Width (label) | 16 | 26 | 115 | 21 | |
Width (text field) | 24 | 18 | 16 | 30 | |
Colorspace (label) | 16 | 26 | 115 | 21 | |
Colorspace (text field) | 24 | 18 | 16 | 30 |
ImageInformationViewController.swift
class. Name the outlets imageHeight
, imageWidth
, and imageColorSpace
.ImageInformationViewController.swift
file to display information on the image file.
var imageBeingDisplayed:UIImage!
viewDidLoad
method in the ImageInformationViewController.swift
file to resemble the following:override func viewDidLoad() {
super.viewDidLoad()
let imageSize = imageBeingDisplayed.size
let height = imageSize.height
let width = imageSize.width
imageHeight.text = "\(height)"
imageWidth.text = "\(width)"
imageColorSpace.text = "RGB"
}
ImageInformationViewController.swift
file should now resemble the following:
import UIKit
class ImageInformationViewController: UIViewController {
@IBOutlet weak var imageHeight: UITextField!
@IBOutlet weak var imageWidth: UITextField!
@IBOutlet weak var imageColorSpace: UITextField!
var imageBeingDisplayed:UIImage!
override func viewDidLoad() {
super.viewDidLoad()
let imageSize = imageBeingDisplayed.size
let height = imageSize.height
let width = imageSize.width
imageHeight.text = "\(height)"
imageWidth.text = "\(width)"
imageColorSpace.text = "RGB"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
Popover Presentation
from the list.imageInformationSegue
.Test your app in the iOS Simulator.
Click the Run button in the Xcode toolbar. Alternatively, you can select Project Run.