

Advanced Navigation Support for SwiftUI.
Navigator 2.1.5
Navigator provides SwiftUI with a simple yet powerful navigation layer based on NavigationStack.
This is not just another push/pop navigation stack library. It supports...
- Simple and easy navigation linking and presentation of views.
- Coordination patterns with well-defined separation of concerns.
- Extensive support for modular applications including cross-module navigation and views.
- True deep linking and internal application navigation via navigation send.
- Easily returning to a specific spot in the navigation tree via navigation checkpoints.
- Returning callback values via navigation checkpoints.
- Both Declarative and Imperative navigation and control.
- Navigation state restoration.
- Event logging and debugging.
- No navigationDestination registration operations are required.
You might want to reread that last bullet point again...
Navigator 2.0 is written entirely in Swift and SwiftUI, supports Observation, and runs on iOS 17 and above.
The Code
Defining Navigation Destinations
Destinations (or routes) are typically just public lists of enumerated values, one for each view desired.
nonisolated public enum HomeDestinations: NavigationDestination {
case page2
case page3
case pageN(Int)
}
SwiftUI requires navigation destination values to be Hashable, and so do we.
With our enum in place, we now need to provide each destination with a variable that returns the correct view for each case. That's easy, since NavigationDestination also conforms to View!
Just provide the view body as part of the enumeration.
...
case pageN(Int)
public var body: some View {
switch self {
case .page2:
HomePage2View()
case .page3:
HomePage3View()
case .pageN(let value):
HomePageNView(number: value)
}
}
}
Note how associated values can be used to pass parameters to views as needed.
To build views that have external dependencies or that require access to environmental values, see Advanced Destinations below.
Managed Navigation Stacks
The next step is to use ManagedNavigationStack when you once used NavigationStack in your code.
struct RootView: View {
var body: some View {
ManagedNavigationStack {
List {
NavigationLink(to: HomeDestinations.page3) {
Text("Link to Page 3!")
}
}
}
}
}
It's that simple.
For the common case of a text-only link, skip the trailing closure entirely. As of Navigator 2.1.2, NavigationLink accepts a title directly alongside the destination.
// Trailing-closure form
NavigationLink(to: HomeDestinations.page3) { Text("Link to Page 3!") }
// Text shorthand
NavigationLink("Link to Page 3!", to: HomeDestinations.page3)
The title argument accepts a plain string, a LocalizedStringKey, or a LocalizedStringResource, so localized titles work without any extra ceremony.
ManagedNavigationStack creates a NavigationStack for you and installs the associated Navigator environment variable that "manages" that particular NavigationStack. It provides it with the NavigationPath and also supports navigation options like automatically presenting sheets and fullScreenCovers.
Those with sharp eyes might have noticed something missing in the above code. We're using NavigationLink with a destination value, but where's the .navigationDestination(for: HomeDestinations.self) { ... ) modifier?
Or, as done in earlier versions of Navigator, the .navigationDestination(HomeDestinations.self) modifier?
As of Navigator 1.2.0, there's no need for them.
Seriously.
Eliminating Navigation Destination Registrations
As you're no doubt aware, SwiftUI's NavigationStack requires destination types to be registered in order
for NavigationLink(value:label:) transitions to work correctly.
But that seems redundant, doesn't it? Our NavigationDestination enumerations already define the views to be provided, so why is registration needed?
Turns out that it's not!
Just use NavigationLink(to:label) instead of NavigationLink(value:label) in your code and let Navigator handle the rest:
import NavigatorUI