In iOS growth, navigation view is unquestionably one of the vital generally used elements. When SwiftUI was first launched, it got here with a view known as NavigationView
for builders to construct navigation-based consumer interfaces. With the discharge of iOS 16, Apple has deprecated the outdated navigation view and launched a brand new view referred to as NavigationStack
to current a stack of views. Most significantly, builders could make use of this new view to construct information pushed navigation.
The Previous Manner of Navigation Views
Previous to iOS 16, you create a navigation interface utilizing NavigationView
and NavigationLink
like this:
NavigationView { Â Â Â Â NavigationLink { Â Â Â Â Â Â Â Â Textual content(“Vacation spot”) Â Â Â Â } label: { Â Â Â Â Â Â Â Â Textual content(“Faucet me”) Â Â Â Â } } |
This creates a fundamental navigation based mostly interface with a Faucet me button. When tapped, the app navigates one stage all the way down to show the vacation spot view.

Working with NavigationStack
Ranging from iOS 16, you exchange NavigationView
with the brand new NavigationStack
. You possibly can maintain the NavigationLink
intact and obtain the identical consequence.
NavigationStack { Â Â Â Â NavigationLink { Â Â Â Â Â Â Â Â Textual content(“Vacation spot”) Â Â Â Â } label: { Â Â Â Â Â Â Â Â Textual content(“Faucet me”) Â Â Â Â } } |
The identical piece of the code may also be written like this:
NavigationStack { Â Â Â Â NavigationLink(“Faucet me”) { Â Â Â Â Â Â Â Â Textual content(“Vacation spot”) Â Â Â Â } } |
We normally use navigation views to construct a master-detail circulate for a listing of knowledge gadgets. Right here is an instance:
var physique: some View {
NavigationStack {
Listing(bgColors, id: .self) { bgColor in
NavigationLink {
bgColor
.body(maxWidth: .infinity, maxHeight: .infinity)
} label: {
Textual content(bgColor.description)
}
}
.listStyle(.plain)
.navigationTitle(“Coloration”)
}
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
struct ContentView: View {     non-public var bgColors: [Color] = [ .indigo, .yellow, .green, .orange, .brown ]      var physique: some View {          NavigationStack {             Listing(bgColors, id: .self) { bgColor in                 NavigationLink {                     bgColor                         .body(maxWidth: .infinity, maxHeight: .infinity)                 } label: {                     Textual content(bgColor.description)                 }              }             .listStyle(.plain)              .navigationTitle(“Coloration”)         }      } } |
This creates a navigation view to show a listing of colour gadgets. When an merchandise is chosen, the app navigates to the element view and reveals the colour view.

Worth-based Navigation Hyperlinks
NavigationStack
introduces a brand new modifier known as navigationDestination
that associates a vacation spot view with a introduced information sort. The identical piece of code within the earlier part may be rewritten like this:
NavigationLink(worth: bgColor) {
Textual content(bgColor.description)
}
}
.listStyle(.plain)
.navigationDestination(for: Coloration.self) { colour in
colour
.body(maxWidth: .infinity, maxHeight: .infinity)
}
.navigationTitle(“Coloration”)
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
NavigationStack {     Listing(bgColors, id: .self) { bgColor in          NavigationLink(worth: bgColor) {             Textual content(bgColor.description)         }      }     .listStyle(.plain)      .navigationDestination(for: Coloration.self) { colour in         colour             .body(maxWidth: .infinity, maxHeight: .infinity)     }      .navigationTitle(“Coloration”) } |
You continue to use NavigationLinks
to current the checklist of knowledge and implement the navigation function. What’s distinction is that every NavigationLink
associates with a price. On prime of that, we added the brand new navigationDestination
modifier to seize the worth change. When a consumer selects a selected hyperlink, the navigationDestination
modifier presents the corresponding vacation spot view for navigation hyperlinks that current information of sort Coloration
.
For those who check the app within the preview, it really works precisely the identical as earlier than. Nonetheless, the interior implementation already makes use of the brand new navigationDestination
modifier.
A number of Navigation Vacation spot Modifiers
You might be allowed to outline a couple of navigationDestination
modifier for dealing with several types of the navigation hyperlinks. Within the earlier instance, we had a single navigationDestination
modifier for the Coloration
sort. Let’s say, we now have one other set of navigation hyperlinks for the String
sort like this:
NavigationLink(worth: systemImage) {
Textual content(systemImage.description)
}
}
.listStyle(.plain)
Listing(systemImages, id: .self) { systemImage in      NavigationLink(worth: systemImage) {         Textual content(systemImage.description)     }  } .listStyle(.plain) |
The systemImages
variable shops an array of the system picture names.
non-public var systemImages: [String] = [ “trash”, “cloud”, “bolt” ] |
On this case, we now have two forms of navigation hyperlinks. One is for the Coloration
sort, the opposite is the String
sort. To deal with the navigation of the String
sort, we will embed one other navigationDestination
modifier to the stack like this:
.navigationDestination(for: String.self) { systemImage in     Picture(systemName: systemImage)         .font(.system(dimension: 100.0)) } |
Now if the consumer faucets one of many system picture names, it navigates to a different view that shows the system picture.

Working with Navigation States
In contrast to the outdated NavigationView
, the brand new NavigationStack
means that you can simply maintain observe of the navigation state. The NavigationStack
view has one other initialization methodology that takes in a path
parameter, which is a binding to the navigation state for the stack:
init( Â Â Â Â path: Binding<Knowledge>, Â Â Â Â root: () –> Root ) the place Knowledge : MutableCollection, Knowledge : RandomAccessCollection, Knowledge : RangeReplaceableCollection, Knowledge.Ingredient : Hashable |
If you wish to retailer or handle the navigation state, you may create a state variable. Here’s a code pattern:
@State non-public var path: [Color] = []
var physique: some View {
NavigationStack(path: $path) {
Listing(bgColors, id: .self) { bgColor in
NavigationLink(worth: bgColor) {
Textual content(bgColor.description)
}
}
.listStyle(.plain)
.navigationDestination(for: Coloration.self) { colour in
VStack {
Textual content(“(path.rely), (path.description)”)
.font(.headline)
HStack {
ForEach(path, id: .self) { colour in
colour
.body(maxWidth: .infinity, maxHeight: .infinity)
}
}
Listing(bgColors, id: .self) { bgColor in
NavigationLink(worth: bgColor) {
Textual content(bgColor.description)
}
}
.listStyle(.plain)
}
}
.navigationTitle(“Coloration”)
}
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
struct ContentView: View {     non-public var bgColors: [Color] = [ .indigo, .yellow, .green, .orange, .brown ]      @State non-public var path: [Color] = []      var physique: some View {          NavigationStack(path: $path) {             Listing(bgColors, id: .self) { bgColor in                  NavigationLink(worth: bgColor) {                     Textual content(bgColor.description)                 }              }             .listStyle(.plain)              .navigationDestination(for: Coloration.self) { colour in                 VStack {                     Textual content(“(path.rely), (path.description)“)                         .font(.headline)                      HStack {                         ForEach(path, id: .self) { colour in                             colour                                 .body(maxWidth: .infinity, maxHeight: .infinity)                         }                      }                      Listing(bgColors, id: .self) { bgColor in                          NavigationLink(worth: bgColor) {                             Textual content(bgColor.description)                         }                      }                     .listStyle(.plain)                  }             }              .navigationTitle(“Coloration”)          }      } } |
The code is much like the earlier instance. We added a state variable named path
, which is an array of Coloration
, to retailer the navigation state. In the course of the initialization of NavigationStack
, we cross its binding for managing the stack. The worth of the path
variable can be mechanically up to date when the navigation stack’s state modifications.
I made a minor change for the navigation vacation spot. It shows the consumer’s chosen colours and reveals one other checklist of colours for additional choice.

Within the code above, we now have this line of code to show the trail content material:
Textual content(“(path.rely), (path.description)“) |
The rely
property provides you the variety of ranges of the stack, whereas the outline presents the present colour. Say, for instance, you first choose the colour indigo after which additional selects yellow. The worth of rely
is 2, which implies the navigation stack has two ranges.
With this path
variable, you may programmatically management the navigation of the stack. Let’s say, we will add a button for customers to leap on to the basis stage of the stack. Right here is the pattern code:
Button { Â Â Â Â path = .init() } label: { Â Â Â Â Textual content(“Again to Foremost”) } .buttonStyle(.borderedProminent) .controlSize(.massive) |
By resetting the worth of the path
variable, we will instruct the navigation stack to return to the basis stage.
As it’s possible you’ll already conscious, we will manipulate the worth of the path
variable to regulate the state of the navigation stack. For instance, when the ContentView
seems, the app can mechanically navigate down three ranges by including three colours to the path
variable like this:
NavigationStack(path: $path) { Â Â . Â Â . Â Â . } .onAppear { Â Â Â Â path.append(.indigo) Â Â Â Â path.append(.yellow) Â Â Â Â path.append(.inexperienced) } |
Whenever you launch the app, it mechanically navigates down three ranges. That is how one can management the navigation state programmatically and an effective way to deal with deep linking.

Abstract
The brand new NavigationStack
, launched in iOS 16, permits builders to simply construct data-driven navigation UI. In case your app doesn’t have to assist older variations of iOS, you may make the most of this new part to deal with deep linking and complicated consumer flows.