What is the correct way to combine the mentioned views.
As of now, i have a NavigationStack
at the bottom of my app. It displays a LaunchView
as root. When a user is authenticated, the main view is added to the stack, if not, the login/ register views are added to the stack. This works fine.
NavigationStack(path: self.$vm.path) {
LaunchView()
.navigationDestination(for: Authentication.self) { value in
switch value {
case .login:
LoginView(vm: LoginViewModel() { type, action in
...
})
case .verify: // let .verify(email)
VerifyMailView(vm: VerifyMailViewModel() { type, action in
...
})
case .authenticated:
AuthenticatedView() { type, action in
self.vm.set(type: type) { value in
...
}
}
}
}
}
The AuthenticatedView
consists of a TabView
with three views. Here comes the issue. I assumed i could set the title and toolbars of the three views directly on them, which is not the case. However i don't want nor can set the titles or the toolbars within the tabview, as they require data from the views viewmodels (to which the TabView
has, and should not have, no access).
TabView(selection: self.$vm.index) {
DiscoverView(vm: DiscoverViewModel(account: self.vm.$account, type: self.type))
.tabItem {
Image(self.vm.index == 0 ? "discover.selected" : "discover")
}
.tag(0)
MatchesView(vm: MatchesViewModel(type: self.type))
.tabItem {
Image(self.vm.index == 1 ? "matches.selected" : "matches")
}
.tag(1)
ProfileView(vm: ProfileViewModel(account: self.vm.$account, type: self.type))
.tabItem {
Image(self.vm.index == 2 ? "profile.selected" : "profile")
}
.tag(2)
}
.toolbar(.hidden, for: .navigationBar)
The only workaround i found is to hide the navigationbar in the TabView
and to set new NavigationView
s within the child views.
NavigationView {
VStack {
...
}
.navigationTitle(Text("discover"))
.toolbar {
...
}
}
This, however, feels not correct as it is a NavigationView inside a NavigationView and even is buggy (the title and toolbar are sometimes placed below their normal position, kind of like below the hidden, but still exisiting navigation bar of the TabView
).
Thus, has anyone found a solution of how to properly combine a NavigationStack
with a TabView
?
TabView
cannot be inside aNavigationStack
but eachtabItem
can have its ownNavigationStack
. This is in the apple guidelines. If you have aTabView
it should always be visible. People can get around this by making their own but apple does not provide this functionality.NavigationStack
and aNavigationView
. You are saying that one can never use both, aNavigationStack
and aTabView
, within an app. This would mean you would miss out on programatic navigation if you were to implement aTabView
, which does not make sense to me.NavigationStack
not aNavigationView
. And it makes no sense to create multiple stacks for each tab, as there should only be one.