NavigationStack: Programmatic Navigation in SwiftUI

Think of NavigationStack as a pile of views. You push new views onto the stack programmatically by appending to a path array. This is SwiftUI's modern, data-driven way to handle deep navigation, replacing the older, buggier NavigationView.
Why it exists
SwiftUI's original NavigationView was difficult to control programmatically. Developers struggled to push or pop views based on state changes, leading to complex workarounds. NavigationStack was introduced to solve this by providing a robust, data-driven API where the navigation hierarchy is a direct reflection of your app's state.
The mental model
Think of NavigationStack as a pile of plates, where each plate is a view. You can only add a new plate to the top (pushing a view) or take the top one off (popping a view). The entire pile of plates is represented by a simple array of data you own, called the "path". To change what's on screen, you just modify the array.
How it works
You initialize a NavigationStack with a binding to a path, which is an array of hashable data (e.g., [Int], [String], or custom structs/enums). To push a new view, you use a NavigationLink with a value parameter. When a user taps it, the link's value is appended to your path array. The NavigationStack observes this change and uses a .navigationDestination(for: DataType.self) modifier to find the right view to build for that piece of data. To go back, the system automatically removes the last item from the path. You can also do this programmatically by simply calling path.removeLast().
When to use it
Use NavigationStack for any hierarchical navigation flow, especially those that are more than one level deep. It excels in drill-down interfaces (like a file browser), master-detail patterns, and any scenario where you need to programmatically control the navigation path, such as restoring state or responding to a deep link from a notification.
When not to use it
For flat navigation structures, like a main screen with different sections, use TabView. For presenting a single, self-contained view modally (like a compose screen or a login form), use the .sheet() or .fullScreenCover() modifiers. NavigationStack is specifically for managing a stack of related views.
One canonical example
An e-commerce app's product browsing flow. The root view is a list of product categories. Tapping "Electronics" pushes a view for that category, and the path might become [Category(id: "electronics")]. Tapping a "Laptops" sub-category pushes another view, and the path becomes [Category(id: "electronics"), Category(id: "laptops")]. The entire user journey is captured in one state variable.
Interview question
Which approach best describes how NavigationStack programmatically manages its view hierarchy?
- a.It binds to an array of hashable data, where changes to the array reflect the navigation path.Correct
- b.It uses a global navigation manager to push and pop views based on user actions.
- c.It directly manipulates the view tree by adding and removing child views as needed.
- d.It relies on EnvironmentObject to pass navigation state down the view hierarchy.
Why? this is the answer
NavigationStack is data-driven, using a binding to an array (the 'path') to represent the navigation stack. Modifying this array directly controls the views on screen, unlike imperative global managers or direct view tree manipulation.
Just read this? Test yourself on what you have been reading.
Read the original → developer.apple.com
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on swiftui — each one lists the topics its interview covers.
See open roles