SwiftUIMidOpen-ended

How do you handle navigation in SwiftUI using `NavigationStack`?

Explanation & Code

Answer: NavigationStack (iOS 16+) replaces NavigationView and uses a path-based approach for programmatic navigation, making deep linking and state-driven navigation much cleaner.

Code Example:

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

    var body: some View {
        NavigationStack(path: $path) {
            HomeView()
                .navigationDestination(for: Article.self) { article in
                    ArticleDetailView(article: article)
                }
                .navigationDestination(for: User.self) { user in
                    UserProfileView(user: user)
                }
        }
    }
}

// Navigate programmatically
path.append(someArticle)   // push
path.removeLast()          // pop
path = NavigationPath()    // pop to root

Rate your understanding:

Ready to practice more SwiftUI?

Test yourself with our interactive quiz mode or browse all curated questions for this topic.