I noticed that removing more than 1 view from a NavigationStack (using .removeLast(k)
on its NavigationPath, where k > 1) is not animated at all. I was expecting to see the previous view sliding right, revealing to root view.. but what I get instead is the previous view disappearing without any animation whatsoever. (however, the animation plays as expected when removing only 1 view from the stack (k = 1))
In the example below, the animation plays when the reset button is tapped from page 2, but it does not from page 3, 4 and so on
Any help would be appreciated! Thank you
import SwiftUI
struct ContentView: View {
@State var path = NavigationPath()
var body: some View {
VStack {
NavigationStack(path: $path) {
Subview()
.navigationDestination(for: Int.self) { n in
Subview(n:n)
}
}
// reset button
Button(action: onTap) {
Text("RESET")
}
.buttonStyle(.borderedProminent)
}
}
func onTap() {
if !path.isEmpty {
let n = path.count
path.removeLast(n)
}
}
}
struct Subview: View {
var n: Int = 1
var body: some View {
NavigationLink(value: n+1) {
Text("go to page \(n+1)")
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}