I have an app using TabView with multiple Tabs using tabViewStyle „sidebarAdaptable“. Each Tab does have its own NavigationStack.
When I switch multiple times between the tabs and append a child view to one of my navigation stacks, it will stop working after a few tries with the following error „A NavigationLink is presenting a value of type “HomeStack” but there is no matching navigationDestination declaration visible from the location of the link. The link cannot be activated. Note: Links search for destinations in any surrounding NavigationStack, then within the same column of a NavigationSplitView.“
The same code is working fine on iOS, iPadOS but not working on macOS. When I remove tabViewStyle of sidebarAdaptable it’s also working on macOS.
I shrinked it down to a minimal reproducible code sample. Any idea if that is a bug or I'm doing something wrong?
struct ContentView: View {
@State private var appState: AppState = .init()
var body: some View {
TabView(selection: $appState.rootTab) {
Tab("Home", systemImage: "house", value: RootTab.home) {
NavigationStack(path: $appState.homeStack) {
Text("Home Stack")
NavigationLink("Item 1", value: HomeStack.item1)
.padding()
NavigationLink("Item 2", value: HomeStack.item2)
.padding()
.navigationDestination(for: HomeStack.self) { stack in
Text("Stack \(stack.rawValue)")
}
.navigationTitle("HomeStack")
}
}
Tab("Tab1", systemImage: "gear", value: RootTab.tab1) {
NavigationStack(path: $appState.tab1Stack) {
Text("Tab 1 Stack")
NavigationLink("Item 1", value: Tab1Stack.item1)
.padding()
NavigationLink("Item 2", value: Tab1Stack.item2)
.padding()
.navigationDestination(for: Tab1Stack.self) { stack in
Text("Stack \(stack.rawValue)")
}
.navigationTitle("Tab1Stack")
}
}
}
.tabViewStyle(.sidebarAdaptable)
.onChange(of: appState.rootTab) { _, _ in
appState.homeStack.removeAll()
appState.tab1Stack.removeAll()
}
}
}
@MainActor
@Observable
class AppState {
var rootTab: RootTab = .home
var homeStack: [HomeStack] = []
var tab1Stack: [Tab1Stack] = []
}
enum RootTab: Hashable {
case home
case tab1
case tab2
}
enum HomeStack: String, Hashable {
case home
case item1
case item2
}
enum Tab1Stack: String, Hashable {
case home
case item1
case item2
}