When using a NavigationStack
, the path is updated immediately when navigation happens via NavigationLink
, but a change to the path triggered via the back button can only be seen (detected programmatically) after the transition animation ran.
I want update another view that’s next to the NavigationStack on iPad in parallel when navigation happens, but with the obvious means this always happens with a significant delayed when navigation happens through the back button.
Is this possible? Is there a way to detect a change to the path immediately when it happens in all cases?
Code snippet to observe the update behavior of the path-Binding of a NavigationStack:
@Observable class ExampleModel {
var navPath: [Int] = [] {
didSet {
print("navPath changed: ", navPath)
}
}
}
struct ContentView: View {
@State var model = ExampleModel()
var body: some View {
NavigationStack(path: $model.navPath) {
ListExampleView()
.navigationDestination(for: Int.self) { value in
Text(String(value))
}
}
}
}
struct ListExampleView: View {
var body: some View {
List {
ForEach(0 ..< 30) { item in
NavigationLink(value: item) {
Text("Hello, world!")
}
}
}
.navigationBarTitle("Example")
.listStyle(.plain)
}
}
#Preview {
ContentView()
}