UIPresentationController is the presentation management class that backs view controllers presentation. From the time a view controller is presented until the time it is dismissed, UIKit uses a presentation controller to manage various aspects of the presentation process for that view controller. The presentation controller can add its own animations on top of those provided by animator objects, respond to size changes, and manage other aspects of how the view controller is presented onscreen. With iOS8, Apple improved alerts and action sheets by introducing UIAlertController. If you are interested in learning about UIAlertController, you can read more about it in this post that I published a couple of months ago: UIAlertController: Alert and Action Sheet in iOS.

With this post, I’m going to talk about popovers and search, other presentation controllers that were improved with iOS 8. Let’s dive right in!

Popovers

UIPopoverPresentationController replaces UIPopoverController in iOS 8. It’s functionally equivalent to the UIPopoverController, but to facilitate the view controller adapting in a changing environment, it has an additional feature: built-in adaptivity. This means that the popover presentation controller will handle the transition between regular and compact environments: on an iPad, it will be displayed as a popover and on an iPhone it will appear as a modal, covering the screen completely. The problem with the old UIPopoverController was that, if you had a Universal app, you had to keep a different code base. So you would use:

or

method if the device was an iPad, and presentViewController: for an iPhone.

In iOS 8, Apple fixed this problem and here is how you use the new API:
you first set the view controller’s modelPresentationStyle to UIModalPresentationPopover, you call the presentViewController: animated: completion: method and finally, you create the presentation controller and modify its properties. When configuring popovers, you have to specify the popover’s anchor point. You can do this by specifying either a bar button item or a source view or a source rectangle as the anchor point for the popover. You can configure other properties as well to accommodate the presented content.

Let’s see an example

Let’s build an example with the new popover presentation controller.

Return to the example that we were building here, add a new file, Cocoa Touch Class, name it ContentViewController, subclass of UIViewController, also create the XIB file and choose the language that you have been using so far, Objective-C or Swift. Go the ContentViewController.xib and change the background color so it’s easier to recognize what we are seeing in the screen.

Back in ViewController, if you are using Objective-C, import the new view controller. Skip this step if you are using Swift.

Add an IBOutlet and connect it to the UIButton in the Main.storyboard

Then, add or replace the action associated to the UIButton with the following:

In line 12, I initialize my view controller “ContentViewController” with the nib file. In line 13, I choose the presentation style in which the content is displayed in a popover view. When in a horizontally regular environment, the background content is dimmed and taps outside the popover cause the popover to be dismissed. Then, in a horizontally compact environment, the presented view covers the screen. In line 14, I get the popover presentation controller. I configure the popover with lines 15, 16 and 17, anchoring the popover to the UIButton that we added and the preferred arrow direction for the popover. In line 18 I set the delegate that handles popover-related messages to the view controller, and in line 19, I finally present the popover presentation controller. In line 20, the delegate method returns the presentation style to use when the specified set of traits are active. In line 21 the delegate returns the navigation controller as the view controller to display when adapting to the specified presentation style.

Build and run and press the popover button. Note the different behavior of the popover depending on the device the app is running.

In iPhones (except iPhone 6/6s Plus), the popover will cover the entire screen and will behave like a modal view controller, , covering the entire screen, in portrait and landscape. Now, to dismiss the view controller on the iPhone, you need to add something to trigger that action (UIButton, UIBarButton, UITableView). I added a UIBarButton for this demo.

In the iPhone 6/6s Plus, the popover will behave like a modal view controller in portrait and like a popover presentation controller in landscape.

popover-controller-iphone-6s-plus-landscape

In the iPad, the popover will appear anchored to the button that we have specified in our code. Taps outside the popover cause the popover to be dismissed, so you can avoid adding any button. However, remember that you need it for the iPhone 6/6s Plus since taps outside the popover do not cause the popover to be dismissed.

popover-controller-ipad-portrait

Search

UISearchController replaces To overcome the deficiencies introduced with adaptive environments and to give developers more control over how the search is displayed in an application, UISearchDisplayController in iOS 8. Now there are some additional features: UISearchController can present on any view controller and not only on the full screen view controllers. It also has the capacity to become adaptive and you can influence the animation through a custom animator object.

Conclusion

In this tutorial, I have shown you how to use UIPopoverPresentationController and UISearchController, both introduced with iOS 8. Everything is a view controller now. This gives iOS developers more control over how view controllers are displayed in their apps. We now have adaptivity for your view controllers to work with all class sizes.

Keep coding,

Eva

Eva Diaz-Santana (@evdiasan) is cofounder of InvasiveCode. She develops iOS applications and teaches iOS development since 2008. She also worked at Apple as Cocoa Architect and UX designer.

iOS Consulting | INVASIVECODE

iOS Training | INVASIVECODE