Users click or tap a navigation link to present a view inside a NavigationView. You control the visual appearance of the link by providing view content in the link’s trailing closure. For example, you can use a Label to display a link:
The following NavigationView presents three links to color detail views:
NavigationView {List {NavigationLink("Purple", destination: ColorDetail(color: .purple))NavigationLink("Pink", destination: ColorDetail(color: .pink))NavigationLink("Orange", destination: ColorDetail(color: .orange)) } .navigationTitle("Colors")Text("Select a Color") // A placeholder to show before selection.}
Optionally, you can use a navigation link to perform navigation programmatically. You do so in one of two ways:
Bind the link’s isActive parameter to a Boolean value. Setting the value to true performs the navigation.
Bind the link’s selection parameter to a value and provide a tag of the variable’s type. Setting the value of selection to tag performs the navigation.
For example, you can create a State variable that indicates when the purple page in the previous example appears:
@Stateprivatevar shouldShowPurple =false
Then you can modify the purple navigation link to bind to the state variable:
Creates a navigation link that presents a destination view when a bound selection variable matches a value you provide, using a text label that the link generates from a localized string key.
Available when Label is Text and Destination conforms to View.
Creates a navigation link that presents a destination view when a bound selection variable matches a value you provide, using a text label that the link generates from a title string.
Available when Label is Text and Destination conforms to View.
Creates a navigation link that presents a destination view when a bound selection variable matches a value you provide, using a text label that the link generates from a localized string key.
Available when Label is Text and Destination conforms to View.
Creates a navigation link that presents a destination view when a bound selection variable matches a value you provide, using a text label that the link generates from a title string.
Available when Label is Text and Destination conforms to View.