Custom UIViewController Transitions: Beyond the Defaults

A custom transition lets you choreograph the handoff between two screens, defining the exact animation. Use it for unique experiences like a card expanding to fill the screen.
WHY IT EXISTS Standard iOS transitions like push and modal are predictable but generic. To create a unique, memorable app experience that reinforces a brand's visual identity, developers need full control over how one screen gives way to another. Custom transitions provide this control.
THE MENTAL MODEL A custom transition is like being a movie director for the change between two view controllers. You are given a stage (the container view) and the actors (the 'from' and 'to' views). Your job is to animate these actors on and off the stage over a specific duration, creating a seamless and visually engaging scene change.
HOW IT WORKS You create an object that conforms to the UIViewControllerAnimatedTransitioning protocol. This requires implementing two key methods. First, transitionDuration(using:) specifies how long the animation takes. Second, animateTransition(using:) is where you define the animation itself. Inside this method, you receive a context object that gives you the container view, the starting view controller, and the destination view controller. You add the 'to' view to the container, run your animations using UIView.animate, and then, critically, you must call context.completeTransition() in the animation's completion block to signal that the handoff is finished.
WHEN TO USE IT Use custom transitions to create a signature look and feel. They are perfect for visually-driven apps where the transition itself is part of the story, such as a photo expanding from a grid into a detail view, a card flipping over to reveal more information, or a circular reveal for a new menu.
WHEN NOT TO USE IT Avoid using custom transitions for every screen change. Overuse can be disorienting, make an app feel gimmicky, and frustrate users who expect standard, fast navigation. For simple, utilitarian flows, the default system transitions are often better because they are familiar and efficient. Also, avoid overly complex animations that are hard to maintain or perform poorly on older devices.
ONE CANONICAL EXAMPLE A common use case is a 'card expansion' from a collection view. A user taps a small card. The transition animator takes a snapshot of that card, hides the real one, and animates the snapshot's frame and properties until it matches the frame of the destination detail view. Upon completion, the snapshot is removed and the real detail view is shown. The reverse animation shrinks the detail view back down to the card's original position in the collection.
Read the original → developer.apple.com
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.