-
-
Save chriseidhof/f7a19206d07ab04335f4156d9e99f40e to your computer and use it in GitHub Desktop.
A couple of notes from testing:
While this causes navigation to pop:
DetailView(showDetail: $levels[1])
These options do not:
MyListDetailView(showDetail: .constant(true))
MyListDetailView(showDetail: .constant(levels[1]))
I also tried:
- Creating and passing an
ObservableObject
- Using
@EnvironmentObject
to share levels data
Neither worked. 🙁
I submitted a Feedback for this (FB9795803).
Thanks. Yes, I have tried those things as well. It's easy to "fix" this by disabling various parts, but in the end, I "only" want navigation that's driven by a single @State property.
Hello @chriseidhof , I think there's related post on Apple Forum. When parent view state changes, Navigation automatically pops out child views (on iOS14.0 it propagated changes to update child views instead AFAIK). Unfortunately I don't have 14.0SDK to verify for this use-case
I don't think SwiftUI is ready for this kind of navigation yet. Navigation is controlled by stated, but it has only 2 states... may be we need more? Somehow there is a missing state for performing the action, etc. I would like some interface like withAnimation...
performNavigation(withAnimaion: .animateLastOnly) {
levels = [true, true]
}
Also it would be awesome if it would work for closing too.
I think for now, you can use workaround:
Button("Navigate To Detail") {
levels = [true, false]
// slow down due to the opening of the first screen
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
levels = [true, true]
}
}
Code with changes:
import SwiftUI
struct DetailView: View {
@Binding var showDetail: Bool
var body: some View {
List {
NavigationLink("detail", isActive: $showDetail) {
Text("Detail")
}
}
}
}
struct MyList: View {
@Binding var levels: [Bool]
var body: some View {
List {
NavigationLink("Detail", isActive: $levels[0]) {
DetailView(showDetail: $levels[1])
}
}
.overlay(
Button("Navigate To Detail") {
levels = [true, false]
// slow down due to the opening of the first screen
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
levels = [true, true]
}
}
)
}
}
struct ContentView: View {
@State var levels = [false, false]
var body: some View {
NavigationView {
MyList(levels: $levels)
}
.navigationViewStyle(.stack)
.overlay(Text(levels.map { $0 ? "t" : "f"}.joined()), alignment: .bottom)
}
}
Tried this myself, even with separate properties for different levels.
Could it be related to double navigation animations happening simultaneously? I mean, UIKit typically throws in warnings when you try to do multiple navigations until animation is complete. However, no warnings in console this time.