I expect back stack to work same as in system navigation: view states from the back stack should be saved.
It includes saving @State and @StateObject annotated objects, but I can think of workaround how I can store them manually.
The main think is scroll view position - in SwiftUI it's impossible to scroll to contentOffset, if we're using basic ScrollView, so I can't even store scroll position and restore it when view appears, unless I write my own scroll view.
Here's a basic example to reproduce:
Run the app
Scroll the list
Click Next button to navigate
Click Back button to navigate back
Scroll position is reset.
@EnvironmentObject private var navigator: Navigator
var body: some View {
SwitchRoutes {
Route("/settings") {
Button {
navigator.goBack()
} label: {
Text("back")
}
}
Route {
Text(navigator.path)
Button {
navigator.navigate("/settings")
} label: {
Text("next")
}
ScrollView {
ForEach(0..<100, id: \.self) { i in
Text("\(i)")
}
}
}
}
}
A possible workaround would be having all the screens from the back stack in the view tree - e.g. in ZStack, one on top of an other one. But this for sure would break onAppear/onDisappear, as well as transitions.
I expect back stack to work same as in system navigation: view states from the back stack should be saved.
It includes saving
@State
and@StateObject
annotated objects, but I can think of workaround how I can store them manually.The main think is scroll view position - in SwiftUI it's impossible to scroll to
contentOffset
, if we're using basicScrollView
, so I can't even store scroll position and restore it when view appears, unless I write my own scroll view.Here's a basic example to reproduce:
A possible workaround would be having all the screens from the back stack in the view tree - e.g. in
ZStack
, one on top of an other one. But this for sure would breakonAppear
/onDisappear
, as well as transitions.