I am experiencing issues using NavigationStack inside an NavigationSplitView. Since the NavigationSplitView doesn't support the new NavigationPath, I presumed I need to use the NavigationStack as detail view for the NSV. However, if I use the NavigationStack with NavigationPath and change content from the sidebar, the end result on iPad OS becomes rather weird. It doesn't push the views correctly, and pops before applying the next view.
Am I doing something wrong? Or am I using the APIs in an unintended way?
This is my code:
struct ContentView: View {
@State private var selectedItem: Int?
@State private var navigationPath: NavigationPath = NavigationPath()
var body: some View {
NavigationSplitView {
List(selection: $selectedItem) {
ForEach(0...10, id: \.self) { item in
Label("Item: \(item)", systemImage: "chevron.right").tag(item)
}
}
} detail: {
NavigationStack(path: $navigationPath) {
VStack {
Text("Hello, world!")
Button {
navigationPath.append("next")
} label: {
Text("Next page")
}
}
.navigationDestination(for: String.self, destination: { text in
Text(text)
})
.navigationDestination(for: Int.self) { int in
VStack{
Text("Item: \(int)")
Button {
navigationPath.append("next")
} label: {
Text("Next page")
}
}
}
}
}.onChange(of: selectedItem) { newValue in
guard let int = newValue else { return }
navigationPath.append(int)
debugPrint("Appended path \(int). Count: \(navigationPath.count)")
}
}
}
The result I am experience is:
Update:
Upon further investigation, it seems that this has to do with the $selectingItem
binding property, if this is set then it messes up the NavigationStack.
For instance, if you change the List
in the code above to the following, it works like a charm:
List {
ForEach(0...10, id: \.self) { item in
Label("Item: \(item)", systemImage: "chevron.right").onTapGesture {
selectedItem = item
}
}
}
I've filed FB11165904