I have recently updated the navigation in my app from using lots of NavigationLinks, because I was seeing errors surrounding multiple pushes to the navigation stack.
My app is now working well with a custom navcontroller that programatically adds views to the stack, but there is one issue with when I try to pop back to the previous view using the back button on the toolbar it always pops all the way back to the root going through all the child views one by one.
Here is an overview of the current setup:
Navigation Controller –
class NavigationController: ObservableObject {
@Published var homeNavPath: [AppView] = []
public func navigateTo(destination: AppView) {
homeNavPath.append(destination)
}
}
Navigation Stack –
NavigationStack(path: $navController.homeNavPath) {
HomeView()
}
Navigation Example –
public struct HomeView: View {
@EnvironmentObject var navController: NavigationController
public var body: some View {
Text("GO TO POST").onTapGesture {
navController.navigateTo(PostView("123"))
}
}
}
public struct PostView: View {
@EnvironmentObject var navController: NavigationController
public var body: some View {
Text("GO TO USER").onTapGesture {
navController.navigateTo(UserView("123"))
}
}
}
public struct UserView: View {
@EnvironmentObject var navController: NavigationController
public var body: some View {
Text("USER INFORMATION...")
}
}
I am able to perfectly navigate from Home -> Post -> User as expected, but when I press the back button on the user page, it will always navigate back to the root (e.g home in this case) no matter how long the navigation path it.
Here is an example, I only press the back button once here:
Let me know if there is any more information that could help and thanks for any suggestions
2
Answers
I took a while but I found the solution.
The issue was where I was declaring my
navigationDestination
.I had the following setup (I had simplified it even more in my question).
There seems to be two ways to fix this. Either I need to call HomeView() directly in the NavigationStack (but I am sure I was not doing that originally for some reason...) or I can move the
navigationDestiation{}
call to be like either of these. All of these fix the issue, although I'm not 100% sure how:or
If anyone has an explanation for why this works that'd be appreciated
I’m unsure what your
AppView
enum looked like, but here’s a working version of the code you provided.A quick note: I would suggest you move
NavigationPath
directly into whichever view owns theNavigationStack
as a@State
property (that way you can useNavigationLink
s and not have to passnavController
throughenvironmentObject
modifiers).