New

🧭 SwiftUI NavigationStack Tutorial for Beginners

Build a three-screen Study Topics app: value-based links, one navigationDestination, a path you control, pop to root and navigation that survives relaunch.

Beginner ⏱️ ~30 minutes 🧰 Swift, SwiftUI, NavigationStack (iOS 16+)
← iOS Track

What You'll Build in This SwiftUI NavigationStack Tutorial

This SwiftUI NavigationStack tutorial builds a small Study Topics app with three screens: a list of topics, a topic screen that lists its lessons, and a lesson screen with buttons that go back one step or all the way to the start. You will learn the modern pattern Apple recommends since iOS 16: links that push values, one navigationDestination that turns values into screens, and a path you can change from code.

The Navigation topic in the iOS track showed NavigationLink(destination:), which is fine for simple apps. The value-based version in this lesson is what you need for "pop to root", deep links and saving where the user was. Apple's migration guide also says to move away from the deprecated NavigationView if your app targets iOS 16 or later.

Prerequisites

  • Xcode with a new iOS App project using SwiftUI, with a deployment target of iOS 17 or later (step 6 uses the two-parameter onChange, which is iOS 17+; everything else works on iOS 16).
  • Basic SwiftUI: views, @State, List and ForEach. The Swift & SwiftUI cheat sheet is a quick refresher.
  • Optional: the Notes app tutorial, which uses a simpler navigation setup you can upgrade with this lesson.

Build the App with NavigationStack Step by Step

1 Create the data model

Values you push onto a navigation stack must be Hashable. We also make them Codable now so we can save the path in step 6.

// Models.swift
import Foundation

struct Lesson: Identifiable, Hashable, Codable {
    let id: Int
    let title: String
    let minutes: Int
}

struct Topic: Identifiable, Hashable, Codable {
    let id: Int
    let name: String
    let lessons: [Lesson]
}

let sampleTopics: [Topic] = [
    Topic(id: 1, name: "Swift Basics", lessons: [
        Lesson(id: 11, title: "Variables and constants", minutes: 8),
        Lesson(id: 12, title: "Optionals", minutes: 12)
    ]),
    Topic(id: 2, name: "SwiftUI Layout", lessons: [
        Lesson(id: 21, title: "VStack, HStack and ZStack", minutes: 10),
        Lesson(id: 22, title: "Lists and ForEach", minutes: 9)
    ])
]

2 Describe every screen with a Route enum

One enum lists every screen that can be pushed. Associated values carry the data each screen needs, like arguments. Swift synthesizes Hashable and Codable for it because Topic and Lesson already conform.

// Route.swift
enum Route: Hashable, Codable {
    case topic(Topic)
    case lesson(Lesson)
    case settings
}

3 Add a NavigationStack with a path

NavigationStack(path:) binds the stack to an array you own. The root view is the topic list. Each NavigationLink(_:value:) appends a Route, and a single navigationDestination(for:) decides which view to show for it.

// ContentView.swift
import SwiftUI

struct ContentView: View {
    @State private var path: [Route] = []

    var body: some View {
        NavigationStack(path: $path) {
            List(sampleTopics) { topic in
                NavigationLink(topic.name, value: Route.topic(topic))
            }
            .navigationTitle("Study Topics")
            .toolbar {
                Button("Settings") { path.append(.settings) }
            }
            .navigationDestination(for: Route.self) { route in
                switch route {
                case .topic(let topic):
                    TopicView(topic: topic)
                case .lesson(let lesson):
                    LessonView(lesson: lesson, path: $path)
                case .settings:
                    Text("Settings").navigationTitle("Settings")
                }
            }
        }
    }
}

Notice that navigationDestination is attached to the List, not placed inside it. Apple's documentation says not to put a navigation destination modifier inside a lazy container like List or LazyVStack, because those only create rows when they are on screen.

4 Build the topic and lesson screens

The topic screen uses the custom-label form, NavigationLink(value:label:). The lesson screen gets the path as a @Binding so it can change navigation from code.

// TopicView.swift
import SwiftUI

struct TopicView: View {
    let topic: Topic

    var body: some View {
        List(topic.lessons) { lesson in
            NavigationLink(value: Route.lesson(lesson)) {
                HStack {
                    Text(lesson.title)
                    Spacer()
                    Text("\(lesson.minutes) min")
                        .foregroundStyle(.secondary)
                }
            }
        }
        .navigationTitle(topic.name)
    }
}

// LessonView.swift
struct LessonView: View {
    let lesson: Lesson
    @Binding var path: [Route]

    var body: some View {
        VStack(spacing: 16) {
            Text(lesson.title).font(.title2).bold()
            Text("About \(lesson.minutes) minutes")
            Button("Back one screen") {
                path.removeLast()
            }
            Button("Back to all topics") {
                path.removeAll()
            }
            .buttonStyle(.borderedProminent)
        }
        .padding()
        .navigationTitle("Lesson")
        .navigationBarTitleDisplayMode(.inline)
    }
}

Run the app. Tapping a topic appends .topic to path, tapping a lesson appends .lesson, and the system back button removes the last value for you. "Back to all topics" empties the array, which is how you pop to the root in SwiftUI.

5 Navigate from code (deep links)

Because the path is just an array, you can open any screen by setting it. This button jumps straight to a lesson with its topic underneath, so Back still works naturally. Add it to the root toolbar in ContentView:

Button("Continue") {
    let topic = sampleTopics[0]
    path = [.topic(topic), .lesson(topic.lessons[1])]
}

The same idea handles a URL in .onOpenURL: parse the URL, build the matching [Route] and assign it to path.

6 Save and restore the navigation path

Since Route is Codable, you can store the path with @SceneStorage so the user returns to the same screen after the system closes the app. Add this to ContentView:

@SceneStorage("studyPath") private var savedPath: Data?

// Attach to the NavigationStack:
.task {
    if let savedPath,
       let restored = try? JSONDecoder().decode([Route].self, from: savedPath) {
        path = restored
    }
}
.onChange(of: path) { _, newPath in
    savedPath = try? JSONEncoder().encode(newPath)
}

In a real app, store IDs in the route instead of whole structs, so a restored path never shows stale data.

7 Mixed types with NavigationPath

If you would rather push Topic and Lesson values directly, without a Route enum, use NavigationPath. It is type-erased, so it can hold different Hashable types, and you add one navigationDestination per type:

struct MixedStackView: View {
    @State private var path = NavigationPath()

    var body: some View {
        NavigationStack(path: $path) {
            List(sampleTopics) { topic in
                NavigationLink(topic.name, value: topic)
            }
            .navigationDestination(for: Topic.self) { topic in
                List(topic.lessons) { lesson in
                    NavigationLink(lesson.title, value: lesson)
                }
            }
            .navigationDestination(for: Lesson.self) { lesson in
                Button("Back to start") { path.removeLast(path.count) }
                    .navigationTitle(lesson.title)
            }
        }
    }
}

NavigationPath has count, isEmpty, append(_:) and removeLast(_:). To save it, use its codable property, which works when every value on the stack is Codable.

How SwiftUI NavigationStack Works

Practice Tasks

  1. Add a "Next lesson" button on LessonView that replaces the last route with the following lesson in the same topic.
  2. Change Route.lesson to carry only a lesson ID and look the lesson up in sampleTopics, showing "Lesson not found" if it is missing.
  3. Show the current path depth in the root toolbar, for example "2 screens deep".

SwiftUI NavigationStack Quiz

Five quick questions on this tutorial.

Frequently Asked Questions

What is NavigationStack in SwiftUI?

NavigationStack is a SwiftUI container, available from iOS 16, that shows a root view and pushes more views on top of it. It replaces the deprecated NavigationView for single-column navigation.

What is the difference between NavigationLink(destination:) and NavigationLink(value:)?

NavigationLink(destination:) builds the next view directly. NavigationLink(value:) only appends a Hashable value, and a matching navigationDestination(for:) modifier decides which view to show, which makes programmatic navigation and deep links possible.

How do I pop to the root view in NavigationStack?

Bind the stack to a path with NavigationStack(path:) and clear it. For an array use path.removeAll(); for a NavigationPath use path.removeLast(path.count).

Why is my navigationDestination not working?

Apple's docs warn not to put navigationDestination inside a lazy container such as List or LazyVStack. Attach it to the List itself or another view outside the lazy container, and make sure the value type matches the one used in NavigationLink.

When should I use NavigationPath instead of an array?

Use an array of one Hashable type, such as an enum of routes, when all screens share one type. Use NavigationPath when you push values of different types, because it is type-erased.

Sources

APIs checked against Apple's SwiftUI documentation on November 22, 2026. NavigationStack, NavigationPath and navigationDestination(for:) are available from iOS 16.

What to Try Next

Keep learning iOS

Head back to the iOS with Swift track for the full learning path and more hands-on projects.

Back to the iOS Track