What is the difference between Modal and Push segue in Storyboards?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
In iOS development with Storyboards, understanding the difference between Modal and Push segues is crucial for delivering a seamless user experience. Both are navigation techniques used to transition between view controllers, but they serve different purposes and involve distinct behaviors and configurations.
Understanding Segues
Before diving into the specifics of Modal and Push segues, let's briefly explore what a segue is. A segue in iOS represents a transition between two view controllers in a storyboard. Segues can be triggered by user interactions, such as tapping a button, or programmatically in the code.
Modal and Push Segues: Differences Explained
Push Segue
A Push segue is commonly used within a UINavigationController and involves a hierarchical navigation structure. It adds a new view controller to the navigation stack, allowing the user to go back to the previous view controller.
Key Characteristics:
- Navigation Hierarchy: Used to move down the hierarchy in a stack-based navigation pathway.
- Back Navigation: A back button is automatically provided at the top of the new view, allowing the user to pop back to the previous view controller.
- Navigation Bar: The new view controller is presented with a navigation bar at the top, which is consistent with the navigation stack.
Example Usage:
Modal Segue
A Modal segue presents a new view controller modally. This means the new view controller is presented over the existing one and is typically used for tasks that require the user's attention, such as input forms or actions.
Key Characteristics:
- Standalone Presentation: Does not use a navigation stack and is not part of any existing hierarchical navigation.
- Dismissal: The presented view must handle its own dismissal, often with a "Cancel" or "Done" button.
- Full-Screen/Partial-Screen View: Can be configured to appear full screen, as a page sheet, or in other presentation styles as needed.
- Overlay: The existing view controller is not removed from view hierarchy, but is overlaid by the newly presented view.
Example Usage:
Key Comparisons
| Aspect | Push Segue | Modal Segue |
| Usage Context | Navigation within a hierarchy | Independent task or need for overlay |
| Navigation Control | Back button provided automatically | Requires manual dismissal |
| View Controller Scope | Part of navigation stack | Presented on top of existing stack |
| Navigation Bar | Appears due to navigation controller | Typically no navigation bar, but customizable |
| Dismissal | Popped off the stack via navigation | Requires explicit dismissal action |
Additional Considerations
Transition Animations
Both Modal and Push segues support custom animations. With a Push segue, the navigation controller typically handles the transition animation as a slide from right to left. For Modal segues, you can define custom transition styles and animations using UIViewControllerAnimatedTransitioning.
Presentation Styles
When using a Modal segue, you can customize the presentation style. Options include full screen, page sheet, or form sheet. The choice of style can affect the user experience and the appearance of underlying view controllers.
Dynamic Data Passing
Both segue types allow for passing data to the destination view controller. This is often done within the prepareForSegue:sender: method, where you cast the destination view controller, then set properties based on the current context.
Best Practices
- Use Push segues for tasks involving navigating deeper into a hierarchical structure.
- Opt for Modal segues when the task is brief or requires a different focus that doesn't fit strictly into the stack-based navigation model.
Understanding when and how to use Modal versus Push segues helps refine the user navigation experience and enables developers to leverage the full potential of Storyboards while aligning with the expectations typical to iOS applications.
Related reading
- What is the difference between Modal and Push segue in Storyboards?
- What is the difference between ObservedObject and StateObject in SwiftUI
- What is the difference between Pan and Swipe in iOS?
- What is the difference between React Native and React?
- What is the difference between static func and class func in Swift?
- What is the difference between View.postDelayed and Handler.postDelayed on the main thread?
- What is the documents directory NSDocumentDirectory?
- What is the easiest way to use SVG images in Android?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.