What is SwiftUI's NavigationStack?
Answer
NavigationStack (iOS 16+) is the modern replacement for NavigationView in SwiftUI, providing a stack-based navigation model with programmatic control. Basic navigation: struct ContentView: View { var body: some View { NavigationStack { List(items) { item in NavigationLink(item.name, value: item) } .navigationDestination(for: Item.self) { item in DetailView(item: item) } .navigationTitle("Items") } } }. Programmatic navigation with NavigationPath: struct ContentView: View { @State private var path = NavigationPath() var body: some View { NavigationStack(path: $path) { HomeView() .navigationDestination(for: Item.self) { item in DetailView(item: item) } .navigationDestination(for: String.self) { id in ProfileView(id: id) } } } // Programmatically navigate: path.append(selectedItem) // Push path.append("user-123") // Push a String route // Go back: path.removeLast() // Pop one path.removeLast(path.count) // Pop to root // Navigate to root: path = NavigationPath(). NavigationLink types: // Value-based (recommended with NavigationStack): NavigationLink("Go to Detail", value: item) // View-based (works anywhere): NavigationLink("Go to Detail") { DetailView() }. Deep linking with NavigationPath: encode/decode NavigationPath using Codable to restore navigation state. vs NavigationView (deprecated): NavigationView had ambiguous behavior on iPad; NavigationStack is explicitly stack-based, more predictable, and supports programmatic navigation out of the box.
Previous
What is the difference between nil coalescing and optional chaining?
Next
What is Core Location in iOS?