Creating Custom And Unwind Segues In UIKit
UIKit navigation often starts with a storyboard segue: one view controller presents another, and the transition is handled automatically. That works well for simple flows, but a polished app may need a card that slides from the bottom, a screen that fades into place, or a reverse animation that returns to an earlier screen.
A custom segue gives you control over how the destination view appears. An unwind segue handles the trip back through an existing view-controller hierarchy, without creating another copy of the previous screen. Used together, they make storyboard-based navigation easier to understand and more consistent.
This is useful for junior iOS developers building portfolio projects, university apps, or client prototypes. A booking app for a Melbourne café, a surf forecast for Perth, or a transport tool used during a Sydney commute can all benefit from navigation that feels deliberate rather than accidental.
| Navigation method | Best use | Main benefit | Common limitation |
|---|---|---|---|
| Show or Present | Standard screen changes | Minimal setup | Limited animation control |
| Push segue | Navigation controller flows | Built-in back button | Requires a navigation stack |
| Custom segue | Branded or unusual transitions | Full animation control | You manage the animation |
| Unwind segue | Returning to an earlier controller | Avoids duplicate screens | Needs a valid unwind action |
| Custom unwind segue | Animated return navigation | Consistent reverse motion | More code and testing |
When A Custom Segue Makes Sense
A segue is a small object responsible for moving from a source view controller to a destination view controller. UIKit creates it from a storyboard connection, then calls perform(). The default segue types cover common behaviour, while a custom subclass lets you define the exact animation.
For example, a modal screen can begin below the visible area and slide upwards. This pattern suits a filter panel, checkout summary, or account sheet. If your app is being demonstrated on an iPhone in a Brisbane co-working space, a clear transition also makes the flow easier to explain to a client or teammate.
A custom segue should have a narrow purpose. It should animate the transition, while the view controllers continue to own their data and screen logic. Avoid placing networking, persistence, or business rules inside perform(). This separation makes the animation reusable and keeps the segue easier to debug.
Building The Transition Class
Create a new Swift file and subclass UIStoryboardSegue. The example below presents the destination without UIKit’s default animation, then moves its view from the bottom of the screen.
import UIKit
final class SlideUpSegue: UIStoryboardSegue {
override func perform() {
guard let sourceView = source.view else { return }
let destinationView = destination.view!
destinationView.frame = sourceView.bounds
destinationView.transform = CGAffineTransform(
translationX: 0,
y: sourceView.bounds.height
)
source.present(destination, animated: false)
UIView.animate(
withDuration: 0.35,
delay: 0,
usingSpringWithDamping: 0.88,
initialSpringVelocity: 0.2,
options: [.curveEaseOut]
) {
destinationView.transform = .identity
}
}
}
In Interface Builder, select the segue between the two controllers, open the Attributes inspector, and set its type to Custom. Enter SlideUpSegue as the class. The custom class name must match the Swift type exactly, including capitalisation.
Use the view’s bounds rather than a hard-coded screen size. This respects different iPhone dimensions, split-screen environments, and rotation. It also prevents awkward results when someone opens the app on an older iPhone SE during a Melbourne tram ride or on a larger Pro Max device.
Connecting An Unwind Segue
An unwind segue travels back to a controller that already exists in the hierarchy. Instead of instantiating a new destination, UIKit searches for a view controller containing a matching action. The action normally has the @IBAction attribute and accepts a UIStoryboardSegue parameter.
Suppose a settings screen was opened from HomeViewController. Add this method to the home controller:
@IBAction func unwindToHome(_ segue: UIStoryboardSegue) {
// Update the home screen if needed
}
In the storyboard, control-drag from the exit icon of the settings controller to unwindToHome(_:). Give the connection an identifier such as unwindToHome. A Cancel button can now use that connection to return to the home screen, while UIKit removes the current controller from the flow.
You can also trigger the configured unwind segue in code:
performSegue(withIdentifier: "unwindToHome", sender: nil)
The source controller must be able to find a valid destination. If the action is missing, has the wrong signature, or is connected to the wrong controller, the exit menu will not offer it. Test the complete path rather than assuming that a visible storyboard connection is sufficient.
Making The Reverse Animation Reliable
A custom unwind segue is useful when the return animation should match the entrance. For a modal presentation, the source controller can dismiss itself while animating downwards:
final class SlideDownUnwindSegue: UIStoryboardSegue {
override func perform() {
guard let sourceView = source.view else {
source.dismiss(animated: false)
return
}
UIView.animate(
withDuration: 0.28,
animations: {
sourceView.transform = CGAffineTransform(
translationX: 0,
y: sourceView.bounds.height
)
},
completion: { _ in
self.source.dismiss(animated: false)
}
)
}
}
Connect the unwind relationship from the source controller to the exit action, then set the segue’s class to SlideDownUnwindSegue. The exact reverse operation depends on how the screen was opened. A modal controller should generally be dismissed, while a controller pushed onto a navigation stack should usually be popped.
Navigation transitions must stay on the main thread because UIKit view updates belong there. If a screen loads remote data before a transition, keep that work separate from the animation; these DispatchQueue threading patterns help prevent a frozen interface. A Brisbane user opening a build over mobile data should still see a responsive transition while content loads.
For production apps, consider UIViewControllerAnimatedTransitioning and UIViewControllerTransitioningDelegate when the animation needs interactive gestures, coordinated navigation-bar changes, or reusable presentation logic. A custom segue is a practical starting point for storyboard projects, but it is not the only transition architecture available in UIKit.
Practical Checks For A Polished Flow
Small navigation defects become obvious when an app is used quickly. Test the first presentation, the return action, repeated opening and closing, and the case where a user taps a button twice. Also check whether the destination has the expected size when safe areas, keyboards, or navigation bars are present.
A useful review list is:
- Confirm that every storyboard segue has the correct custom class and identifier.
- Test both compact and large iPhone layouts, including Dynamic Island and safe-area spacing.
- Verify that an unwind action returns to the existing controller instead of creating a duplicate.
- Keep animation durations short enough for a Melbourne commute or a quick uni demo.
- Check loading and transition behaviour on slower networks and older devices.
Australian App Store users may run an app across a broad mix of devices, and local clients often expect a build to feel ready during a same-day review. Use Australian spelling in visible copy where appropriate, format prices in Australian dollars when the product requires it, and test on the devices your audience actually uses. A regional Western Australia user may also notice a blocked interface sooner than someone connected to fast office Wi-Fi.
A reliable navigation flow has a simple rule: the forward segue presents one clear destination, and the unwind path returns to the correct existing screen. Create a blank UIKit project, add SlideUpSegue, connect two view controllers, and verify the forward and unwind paths before adding more screens.