NavigationStack and Navigation

The interviewer asks

“How does navigation work in SwiftUI? Explain NavigationStack and NavigationLink.”

How to answer

NavigationStack is a push-style container whose state is data: a path of values. NavigationLink(value:) appends a value, and .navigationDestination(for:) maps each value type to a view, so the same push can come from a tap, a deep link or code. For real apps I keep the path in a @MainActor @Observable router (a NavigationPath or [Route]) and inject it via the environment, so any screen can push, pop or reset. Around it, TabView with the Tab API and .sidebarAdaptable handles top-level sections, and NavigationSplitView handles multi-column layouts that collapse to a stack in compact width. NavigationView is soft-deprecated: it still builds with no warning, but new code uses these containers.

Key points

  • NavigationStack(path: $router.path) + NavigationLink(value:) + .navigationDestination(for: Route.self)
  • NavigationPath is type-erased (mixed value types, Codable via path.codable for state restoration); a typed [Route] array is simpler when one enum covers every screen
  • Router: @MainActor @Observable final class Router { var path = NavigationPath() }, owned with @State, shared with .environment(router)
  • Zoom transition: .navigationTransition(.zoom(sourceID:in:)) on the destination + .matchedTransitionSource(id:in:) on the source
  • Tabs: TabView { Tab("Home", systemImage: "house") { … } }, .tabViewStyle(.sidebarAdaptable) (tab bar on iPhone, sidebar on iPad); each tab owns its own NavigationStack
  • Navigation and tab bars adopt Liquid Glass automatically; .tabBarMinimizeBehavior(.onScrollDown) shrinks the tab bar while scrolling

If they dig deeper

Deep linking: onOpenURL parses the URL into Route values and sets router.path. Programmatic pop: path.removeLast(); pop to root: reset the path; inside a destination, @Environment(\.dismiss). navigationDestination(item:) for optional-driven pushes. On iPad, in resized iPhone windows and on iPhone Duo, NavigationSplitView shows columns when the width is regular and collapses to a stack when compact; on iPhone Duo with the iOS 27.1 SDK the app runs edge to edge and tab bars and toolbars lay out vertically, so don't assume a bottom tab bar or a fixed bar height. Use ArrangementView (iOS 27.1+) when you want a primary/secondary arrangement rather than navigation columns.

Common pitfalls

Using NavigationView or the deprecated NavigationLink(isActive:). Declaring navigationDestination inside a lazy container like List rows (it must be on a view that's always present in the stack). Mixing NavigationLink(destination:) with a path-driven stack and expecting the path to reflect it. Nesting NavigationStacks (one per tab, never one inside a pushed screen). Choosing split view vs stack by device type instead of size class.

How to use it

swift
enum Route: Hashable {
    case item(Item)
    case settings
}

@MainActor
@Observable
final class Router {
    var path = NavigationPath()
    func show(_ route: Route) { path.append(route) }
    func popToRoot() { path = NavigationPath() }
}

struct LibraryView: View {
    @State private var router = Router()
    let items: [Item]

    var body: some View {
        NavigationStack(path: $router.path) {
            List(items) { item in
                NavigationLink(item.name, value: Route.item(item))
            }
            .navigationTitle("Library")
            .navigationDestination(for: Route.self) { route in
                switch route {
                case .item(let item): DetailView(item: item)
                case .settings: Text("Settings")
                }
            }
            .toolbar { Button("Settings") { router.show(.settings) } }
        }
        .environment(router)   // child screens: @Environment(Router.self)
    }
}

Top level with the Tab API:

swift
struct AppRoot: View {
    var body: some View {
        TabView {
            Tab("Library", systemImage: "books.vertical") { LibraryView(items: []) }
            Tab("Search", systemImage: "magnifyingglass", role: .search) { Text("Search") }
        }
        .tabViewStyle(.sidebarAdaptable)   // tab bar on iPhone, sidebar on iPad
    }
}

Best practices

  • Drive navigation from data: value-based links, navigationDestination(for:), and a path in an @Observable router
  • Give each tab its own NavigationStack; never nest stacks inside pushed screens
  • Use NavigationSplitView for list/detail and let size classes decide columns vs stack
  • Remove custom bar backgrounds so Liquid Glass bars work; test vertical bars on iPhone Duo (iOS 27.1 SDK)

Related concepts

  • Toolbar, Sheets and fullScreenCover, matchedGeometryEffect / zoom transition, deep linking (onOpenURL), coordinator pattern, NavigationSplitView, ArrangementView, @Observable