UIPresentationController Tutorial: Getting Started

Learn how to build custom view controller transitions and presentations with this UIPresentationController tutorial. By Ron Kliffer.

Leave a rating/review
Download materials
Save for later
Share
You are currently viewing page 4 of 4 of this article. Click here to view the first page.

Overriding the presented controller

There’s still a niggly bit for the use case where you have a view that can only show in regular height. Maybe there’s something on there that’s just too tall to fit in a compact height. UIAdaptivePresentationControllerDelegate can help you.

Set it up by opening SlideInPresentationManager.swift and adding the following method to UIAdaptivePresentationControllerDelegate extension:

func presentationController(
  _ controller: UIPresentationController,
  viewControllerForAdaptivePresentationStyle style: UIModalPresentationStyle
) -> UIViewController? {
  guard case(.overFullScreen) = style else { return nil }
  return UIStoryboard(name: "Main", bundle: nil)
    .instantiateViewController(withIdentifier: "RotateViewController")
}

This method accepts a UIPresentationController and a UIModalPresentationStyle. It returns a view controller that overrides the original controller to present, or nil if the original view controller should be presented.

If the presentation style is .overFullScreen it creates and returns a different view controller controller. This new controller is just a simple UIViewController with an image that tells the user to rotate the screen to portrait.

Build and run the app, bring up the medal count and rotate the device to landscape. You should see the message:

Rotate the device back to portrait to view the medal count again.

Where To Go From Here?

Congratulations! You made it through a world-class athletic journey (for your brain) and built a custom UIPresentationController.

You can download the completed version of the project using Download Materials at the top or bottom of this tutorial.

You’ve covered quite a bit! You learned how to customize and reuse a UIPresentationController to create neat slide-in effects from any direction.

You also learned how to adapt presentations for various devices and both orientations, as well as how to handle cases where the device can’t handle landscape orientation.

I’m sure you can come up with many more creative ideas for presentations in your app. I encourage you to try them out based on what you’ve learned in this tutorial.

To delve deeper into custom transitions, check out this Custom View Controller Presentation Transitions tutorial by Marin Todorov & Fabrizio Brancati. You’ll find some interesting videos on the topic in our Beginning iOS Animation video series.

For more on Adaptive Layouts, check out this adaptive layout in iOS tutorial by Adam Rush, or these adaptive layout video tutorials in the second section of our Mastering Auto Layout video series.

For more information on UIPresentationController, check out Apple’s documentation.

This iOS tutorial was a lot of fun to put together, and I’m keen to hear how it went for you. Bring your questions, blockers and discoveries to the forums below and let’s talk presentations! :]