Consider the following view code:
struct ContentView: View {
@State var activeLink: Int? = 45
var body: some View {
NavigationView {
VStack {
Text("Hello, world!")
.padding()
List(0..<100, id: \.self) { i in
NavigationLink(
tag: i,
selection: $activeLink,
destination: { MyNextView() }
) {
Text("Row nr \(i)")
}
}
}
}
}
}
If the value of activeLink is the same as the tag of a NavigationLink that is currently visible in the UI, the NavigationLink will activate and MyNextView will be presented as expected. However if activeLink has a value equal to a tag of a NavigationLink outside of the visible UI, nothing happens.
This became a problem in my project since I have an ObservableObject representing a "routing state"-model in my app that contains the value of every active NavigationLink in the view hierarchy. The reason for this is that I want to be able to recreate a view hierarchy on app start. However if my routing state wants to activate a NavigationLink outside of the visible UI, as in the example above, it does not work.
I have tried to scroll the list to the NavigationLink-element I want to activate in order to get it to appear but I have not found a smooth way to do this. I tried using the ScrollViewProxy.scrollTo(... in .onAppear(perform: ... of my view but it does not work without a DispatchQueue.main.asyncAfter(deadline: ... which seems very unreliable imo.
Has anyone else come across this problem?