Alle artikelen
12 november 2026

SwiftUI NavigationStack: path-driven navigation explained, 2026

A SwiftUI NavigationStack stores every pushed screen as a value in a path. Bind that path and you can push, replace, reset and restore navigation from code.

Close-up of fingers typing on a laptop keyboard, the hands-on work behind building SwiftUI navigation code

What path-driven navigation means

A SwiftUI NavigationStack keeps a list of values, called the path, with one value for each screen pushed over the root. Bind that list to your own state and you control navigation from code: push, replace, reset and restore.

Node map diagram with four parts: push, replace, reset and restore
Node map: What path-driven navigation means.

That one idea explains most of the API. Deep links, a "back to start" button and restoring a user's place after a relaunch all become edits to a collection. This article covers how the stack works, how links and destinations match, when to pick an array or NavigationPath, and how to save the stack. Everything below is checked against Apple's documentation as of November 2026.

How a SwiftUI NavigationStack works

Apple's documentation describes the stack this way: "Use a navigation stack to present a stack of views over a root view." People add views by tapping a NavigationLink and remove them with a Back button or a swipe gesture. The stack always shows the most recently added view that hasn't been removed, and it doesn't allow the root view to be removed. You can read this in the NavigationStack documentation.

The root view stays put

The root is the first screen, often a list. It is always present. When the root is the only thing visible, the system disables backward navigation controls, because there is nothing left to pop.

The path is the navigation state

By default the stack tracks its own state. You can share that control by creating the stack with init(path:root:) and a binding to a collection you own. The stack adds an item to your collection when it pushes a view and removes one when it pops a view. Your code can observe the collection to read the current stack, and it can change the collection to change what is on screen.

@State private var presentedParks: [Park] = []

NavigationStack(path: $presentedParks) {
    List(parks) { park in
        NavigationLink(park.name, value: park)
    }
    .navigationDestination(for: Park.self) { park in
        ParkDetails(park: park)
    }
}

An empty array means a stack with no pushed views, so only the root shows.

A value-based link does not say which screen to open. It hands the stack a piece of data, and the stack finds the screen by the data's type.

You do this in two parts. First, add a navigationDestination(for:destination:) modifier inside the stack's view hierarchy to associate a data type with a view. Second, create a NavigationLink that presents a value of that same type. In the example above, tapping a link for a Park pushes a ParkDetails view and also appends that park to presentedParks.

For more than one kind of screen, add more than one destination modifier, one for each data type. Apple's documentation states that the stack matches navigation links with navigation destinations based on their respective data types. A Park link and a Trail link can live in the same stack as long as each type has its own destination.

The WWDC22 session The SwiftUI cookbook for navigation puts the same flow in simple terms: tapping a link appends its value to the path, and pressing Back removes the last value.

Typed array or NavigationPath

The path you bind can be an ordinary array or a NavigationPath. The choice depends on how many data types the stack handles.

Use an array when every pushed screen comes from one type, as with [Park] above. It's simple, and the compiler checks it. Use NavigationPath when one stack mixes types. Apple's NavigationPath documentation calls it "a type-erased list of data representing the content of a navigation stack." The path uses type erasure so you can manage a collection of heterogeneous elements, and it provides the usual collection controls for adding, counting and removing data elements.

@State private var path = NavigationPath()

You pay the usual price for type erasure. An array tells you what each element is. A NavigationPath only holds values, so if your code needs to know which screen is on top, track that in your own model. Start with an array and move to NavigationPath when a second data type joins the stack.

Because the path is plain state, navigating from code means changing it. Apple's example replaces the whole stack in one assignment:

func showParks() {
    presentedParks = [Park("Yosemite"), Park("Sequoia")]
}

After this call the stack shows details for Sequoia, the last item in the array. Navigating back removes Sequoia and reveals Yosemite. Two screens appear in one step. Back then behaves as if the person had tapped through them.

To return to the root, clear the path. The WWDC22 session describes this as removing all the items from the path, for example path = [] on an array. Since the root can't be removed, the stack lands on it.

A deep link follows the same pattern. Apple's documentation says to use a path to support deep links, state restoration or other kinds of programmatic navigation. When a link arrives, you translate it into the values that identify the target screen and set the path to them. If your app shares its data model with a web backend, the auth and data contracts for a SwiftUI app that shares a backend with a Next.js site decide which identifiers a link can safely carry.

Saving and restoring the stack

People who leave an app and come back expect to land where they left. A path makes that doable when its values are Codable.

NavigationPath exposes a codable property that returns a serializable representation. Apple's documentation shows an ObservableObject that decodes NavigationPath.CodableRepresentation with a JSONDecoder when it starts, falls back to an empty NavigationPath if decoding fails, and encodes the representation with a JSONEncoder to save it. Because codable is optional, the save method guards against nil. The view saves when the scene phase changes to .background.

The WWDC22 session adds two practical points. Store stable identifiers rather than whole model values, then rebuild the screens from those identifiers with compactMap, so an entry whose item no longer exists, such as a recipe deleted on another device, is dropped instead of crashing the restore. Then persist the encoded data with @SceneStorage.

Common mistakes with NavigationStack

The most common trap is placing the destination in the wrong spot. The WWDC22 session says not to attach navigationDestination inside lazy containers such as List, Table or LazyVGrid, because the stack might not see it. Put the modifier on the container itself, or on a view outside it. For a grid, that means the ScrollView.

The second is mixing eras. The same session says to use NavigationStack and NavigationSplitView in place of the older NavigationView, and notes that old-style programmatic links are deprecated starting in iOS 16. Pick the value-based approach and stay with it across the whole stack.

Conclusion

Treat the path as app state, not a side effect of tapping links. Bind it, give each data type one destination, and every navigation task becomes a small edit to a collection. If you are planning a SwiftUI build, what an iOS app costs to build helps size the work, and automating iOS builds and submission covers shipping it. At Kallos Labs we build iOS apps and the web backends behind them, so if you want a second opinion on an architecture, get in touch.

Frequently asked questions

What is the difference between NavigationStack and NavigationView?

NavigationStack and NavigationSplitView replace NavigationView. Apple's WWDC22 navigation session notes that the old-style programmatic links are deprecated starting in iOS 16. NavigationStack adds value-based links and a path you can read and change.

When should I use NavigationPath instead of an array?

Use NavigationPath when one stack mixes data types. It is a type-erased list with the usual controls for adding, counting and removing. If every screen comes from one type, a plain array is simpler.

How do I go back to the root view in code?

Empty the path, for example path = [] on an array. The root view can't be removed, so the stack ends up showing it.

Can I save the navigation path between launches?

Yes, if the values are Codable. NavigationPath exposes an optional codable representation that you encode and later decode. Guard for nil, and store stable identifiers instead of whole models.