iOS provides several built-in types of transitions. Navigation controllers push and pop to navigate through an information hierarchy, tab bar controllers switch between sections by changing tabs, and any view controller can present and dismiss another view controller modally for specific tasks.
API Overview
Every custom transition involves three main objects:
- The from view controller (the one that’s going away)
- The to view controller (the one that’s appearing)
- An animation controller
Starting a custom transition works the same way as before the transition was customized. For push and pop, it means calling one of the push-, pop-, or set- methods on UINavigationController to modify its stack of view controllers. For changing tabs, it means setting the selectedIndex or selectedViewController property on a UITabBarController. For a modal transition, it means calling ‑[UIViewController presentViewController:animated:completion:] or ‑[UIViewController dismissViewControllerAnimated:completion:]. In each case, this step determines which view controller will be the “from view controller” and which one will be the “to view controller.”
To use a custom transition, you need to say which object should play the role of the animation controller. For me, this was the most confusing part of setting up a custom animated transition because each type of transition looks for the animation controller in a different place. The following table shows how to provide the animation controller for each case. Just remember that you always return the animation controller from a delegate method.
| Transition | Animation controller provided by |
| Push and Pop | The navigation controller’s delegate implements ‑navigationController: animationControllerForOperation:fromViewController:toViewController: |
| Changing Tabs | The tab bar controller’s delegate implements ‑tabBarController: animationControllerForTransitionFromViewController:toViewController: |
| Present | The presented view controller’s transitioningDelegate implements ‑animationControllerForPresentedController:presentingController:sourceController: The presented view controller’s ‑modalPresentationStyle must be set to UIModalPresentationCustom before the transition is started. |
| Dismiss | The presented view controller’s transitioningDelegate implements ‑animationControllerForDismissedController:The presented view controller’s ‑modalPresentationStyle must be setto UIModalPresentationCustom before the transition is started. |
The animation controller can be any object that conforms to the UIViewControllerAnimatedTransitioning protocol. The protocol defines two required methods. One provides the animation duration, and the other performs the animation. Each of these methods is passed a context when it is called. The context provides access to the information and objects you need to create your custom transition. Here are some of the highlights:
- The from view controller
- The to view controller
- The initial and final frames for the from and to view controllers’ views
- The container view, which, according to the docs, “acts as the superview for the views involved in the transition.”
Important: The context also implements ‑completeTransition: which you must call once your custom transition is finished.
That is everything you need to know to make a custom transition.
