How do I use NavigationStack and path-based navigation?

In SwiftUI, NavigationStack is a powerful feature that allows for path-based navigation, making it easy to manage complex navigation flows in your app. By using NavigationStack, you can define a stack of view states and navigate between them dynamically based on your app's needs.

The NavigationStack works in combination with the Path structure to manage navigation state more efficiently. This allows you to push or pop views based on the elements in your path, aiding in creating a more seamless user experience.

Here's a simple example to illustrate how to use NavigationStack with path-based navigation:

struct ContentView: View { @State private var path: [String] = [] var body: some View { NavigationStack(path: $path) { VStack { NavigationLink("Go to Detail 1") { DetailView(detail: "Detail 1") } NavigationLink("Go to Detail 2") { DetailView(detail: "Detail 2") } } } } } struct DetailView: View { let detail: String var body: some View { Text(detail) } }

SwiftUI NavigationStack path-based navigation iOS development app navigation