In my application I am using a custom navigation bar which, besides other functionality, allows the user to navigate back. I am using a NavigationStack with an array with enums as path. On navigating back I simply remove the last element of the path. This is working well as long as the button to navigate back is not clicked while the the navigation stack is not already navigating back i.e. it does not work to quickly click the back button 2+ times. In this case the NavigationStack does not display the destination for the top path, but the previous one.
A simple example showing this behavior:
struct ContentView: View {
@State var navigationPath: [Int] = []
var body: some View {
VStack {
if !navigationPath.isEmpty {
Button {
navigationPath.removeLast()
} label: {
Text("Remove Last")
}
}
NavigationStack(path: $navigationPath) {
NavigationLink(value: 1) {
Text("Navigate to \(1)")
}
.navigationDestination(for: Int.self) { value in
NavigationLink(value: value+1) {
Text("Navigate to \(value+1)")
}
.toolbar(.hidden, for: .navigationBar)
}
}.onChange(of: navigationPath) { newValue in
print("Path: \(newValue)")
}
}
}
}
When clicking the Navigation Link two times (value is 2 -> Navigate to value 3 is shown), followed by two clicks (value is 0) on the back button I would expect the Initial page to be shown again. But while the Navigation bar is reacting as expecting (the Button disappears). It will still show "Navigate to value 2". The path array is updated correctly.