NavigationStack
如果导航堆栈是另一个容器的子容器,则不会调用 .navigationDestination()
。
导航在视觉上发生,但它仅将中间带有典型黄色警告图标的 View 推送到堆栈。如果我将 NavigationStack
作为父 View ,那么它会调用 .navigationDestination()
并且一切都会按预期工作,只是它不满足 UI 要求。
iOS版本:16.2
为什么会这样以及如何解决?
我的代码的近似值。
var body: some View {
VStack {
MyCustomTopBarView()
MyAlwaysOnScreenNavigationStepsProgressBar()
NavigationStack($path) {
contentView()
}
.navigationDestination(for: NavigationState.self) { navigationState in
switch navigationState {
case .screen1:
Text("screen 1")
}
}
.navigationBarHidden(true)
}
}
最佳答案
您只需将 .navigationDestination
添加到 NavigationStack
中的顶级 View ,例如...
enum NavigationState {
case screen1
}
struct ContentView: View {
@State private var path = NavigationPath()
var body: some View {
VStack {
MyCustomTopBarView()
MyAlwaysOnScreenNavigationStepsProgressBar()
NavigationStack(path: $path) {
contentView()
.navigationDestination(for: NavigationState.self) { navigationState in
switch navigationState {
case .screen1:
Text("screen 1")
}
}
.navigationBarHidden(true)
}
Button("Show content") {
path.append(NavigationState.screen1)
}
}
}
func contentView() -> some View {
Text("Content")
}
}
struct MyCustomTopBarView: View {
var body: some View {
Rectangle().fill(.red)
.frame(height: 40)
}
}
struct MyAlwaysOnScreenNavigationStepsProgressBar: View {
var body: some View {
ProgressView()
.progressViewStyle(.linear)
}
}
关于swiftui - 为什么NavigationStack的navigationDestination作为VStack的一部分没有被调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75512392/