I had a bad idea that actually seems to work. The idea is to force the view to refresh once the path is changed. I put an id on the stack and change this whenever the path is updated. It forces the stack to go backwards regardless of what the animation state is.
I would love to get some insight into what exactly is going on with all this. It is strange to me that the path, although a state variable itself, would not force the view to refresh.
I am not confident in this solution, and it doesn't feel right either. I haven't been able to reproduce the crashes I was seeing before. Not sure if that is due to an iOS update or something.
struct ContentView: View {
@State private(set) var path = [String]()
@State private var id = UUID().uuidString
var body: some View {
VStack {
NavigationStack(path: $path) {
Spacer()
VStack {
Spacer()
Button("Go to next") {
path.append(UUID().uuidString)
print(path)
}
.padding()
Spacer()
}
.navigationDestination(for: String.self) { textValue in
VStack {
Text(textValue)
Button("Go to next") {
path.append(UUID().uuidString)
}
}
}
}
.id(id)
Button("Back") {
if path.count > 0 {
path = path.dropLast()
id = UUID().uuidString
}
print(path)
}
.padding()
}
}
}