I have a SwiftUI view that is the landing page for my app with a body like this:
@State private var navigationPath = NavigationPath()
var body: some View {
NavigationStack(path: $navigationPath) {
VStack {
// my UI
}
.navigationDestination(for: NavigationDestination.self) { destination in
switch destination {
case .auth:
AuthView(navigationPath: navigationPath)
}
}
}
}
where NavigationDestination is a private Hashable enum.
After appending NavigationDestination.auth to the navigationPath, I can correctly see my AuthView when it doesn’t include its own NavigationStack:
var body: some View {
VStack {
// my UI
}
.navigationBarBackButtonHidden(true)
}
However, once I wrap it in its own NavigationStack based on the path I passed into it:
var body: some View {
NavigationStack(path: $navigationStack) {
VStack {
// my UI
}
.navigationBarBackButtonHidden(true)
.navigationDestination(for: NavigationDestination.self) { destination in
switch destination {
case .signUp:
SignUpView(navigationPath: navigationPath)
}
}
}
}
(NavigationDestination here is different from the initial view’s, also a private hashable enum)
AuthView no longer is presented and instead it is a view with a yellow triangle and exclamation point. Why does wrapping my second SwiftUI view in its own NavigationStack prevent navigation to it?
When I remove the NavigationStack from my second view, everything works fine, but I cannot navigate in the same manner I did on the first view. Currently, the only solution I see would be to have a RootView that handles all navigation, but I do not like that idea for an app that needs to scale.
ChillOutJimmy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.