Declaration
Initializers
4 SwiftUI initializers back <NavigationLink>. The props you pass pick one.
<NavigationLink
title: string
>
children?: ReactNode
</NavigationLink>@export(implementation) nonisolated init<S>(
_ title: S,
@ContentBuilder destination: () -> Destination
)
where S : StringProtocoltitle*stringA string for creating a text label.
childrenReactNodeContent. Strings become
Text; React Native views are laid out by Yoga and placed by SwiftUI.Swift
destination: () -> Destination
Also takes the common view props: modifiers, style and testID.
Overview
People click or tap a navigation link to present a view inside a
NavigationStack or NavigationSplitView. You control the visual
appearance of the link by providing view content in the link's label
closure. For example, you can use a Label to display a link:
NavigationLink {
FolderDetail(id: workFolder.id)
} label: {
Label("Work Folder", systemImage: "folder")
}For a link composed only of text, you can use one of the convenience
initializers that takes a string and creates a Text view for you:
NavigationLink("Work Folder") {
FolderDetail(id: workFolder.id)
}Link to a destination view
You can perform navigation by initializing a link with a destination view
that you provide in the destination closure. For example, consider a
ColorDetail view that fills itself with a color:
struct ColorDetail: View {
var color: Color
var body: some View {
color.navigationTitle(color.description)
}
}The following NavigationStack presents three links to color detail
views:
NavigationStack {
List {
NavigationLink("Mint") { ColorDetail(color: .mint) }
NavigationLink("Pink") { ColorDetail(color: .pink) }
NavigationLink("Teal") { ColorDetail(color: .teal) }
}
.navigationTitle("Colors")
}Create a presentation link
Alternatively, you can use a navigation link to perform navigation based
on a presented data value. To support this, use the
View.navigationDestination(for:destination:) view modifier
inside a navigation stack to associate a view with a kind of data, and
then present a value of that data type from a navigation link. The
following example reimplements the previous example as a series of
presentation links:
NavigationStack {
List {
NavigationLink("Mint", value: Color.mint)
NavigationLink("Pink", value: Color.pink)
NavigationLink("Teal", value: Color.teal)
}
.navigationDestination(for: Color.self) { color in
ColorDetail(color: color)
}
.navigationTitle("Colors")
}Separating the view from the data facilitates programmatic navigation because you can manage navigation state by recording the presented data.
Control a presentation link programmatically
To navigate programmatically, introduce a state variable that tracks the items on a stack. For example, you can create an array of colors to store the stack state from the previous example, and initialize it as an empty array to start with an empty stack:
@State private var colors: [Color] = []Then pass a Binding to the state to the navigation stack:
NavigationStack(path: $colors) {
// ...
}You can use the array to observe the current state of the stack. You can
also modify the array to change the contents of the stack. For example,
you can programmatically add ShapeStyle.blue to the array, and
navigation to a new color detail view using the following method:
func showBlue() {
colors.append(.blue)
}Coordinate with a list
You can also use a navigation link to control List selection in a
NavigationSplitView:
let colors: [Color] = [.mint, .pink, .teal]
@State private var selection: Color? // Nothing selected by default.
var body: some View {
NavigationSplitView {
List(colors, id: \.self, selection: $selection) { color in
NavigationLink(color.description, value: color)
}
} detail: {
if let color = selection {
ColorDetail(color: color)
} else {
Text("Pick a color")
}
}
}The list coordinates with the navigation logic so that changing the selection state variable in another part of your code activates the navigation link with the corresponding color. Similarly, if someone chooses the navigation link associated with a particular color, the list updates the selection value that other parts of your code can read.
From Apple’s SwiftUI documentation. Code samples there are Swift.
Relationships
Conforms to
- View
See also
More in Navigation.
- LinkA control for navigating to a URL.
- NavigationSplitViewA view that presents views in two or three columns, where selections in leading columns control presentations in subsequent columns.
- NavigationStackA view that displays a root view and enables you to present additional views over the root view.
- ShareLinkA view that controls a sharing presentation.
- TabThe content for a tab and the tab's associated tab item in a tab view.
- TabsTabbed navigation (SwiftUI TabView with Tab). Each Trigger declares a tab; the Content with the same value is its page.
- TabSectionA container that you can use to add hierarchy within a tab view.
- TabViewA view that switches between multiple child views using interactive user interface elements.