问题描述
我在 watchOS 8.1RC 中发现了一个由 TabView 触发的 NavigationLink 的回归.它立即被驳回.
I have discovered a regression in watchOS 8.1RC with NavigationLink triggered from a TabView.It's immediately dismissed.
它在 watchOS 8.0 或模拟器 (watchOS 8.0) 中工作.你知道解决方法吗?谢谢
It was working in watchOS 8.0 or in Simulator (watchOS 8.0).Do you know a workaround ?Thanks
示例代码:
import SwiftUI
@main
struct TestNavigationApp: App {
var body: some Scene {
WindowGroup {
NavigationView {
ContentView()
}
}
}
}
struct ContentView: View {
var body: some View {
List {
NavigationLink(destination: ContentView1()) {
Text("To TabView")
}
}
}
}
struct ContentView1: View {
var body: some View {
TabView {
NavigationView {
NavigationLink(destination: ContentView2()) {
Text("To ContentView2")
}
}
VStack {
Text("Screen2")
}
}
}
}
struct ContentView2: View {
var body: some View {
Text("ContentView2")
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
推荐答案
我在使用 watchOS 8.1(和 8.3 beta)之前遇到了同样的问题.
I'm experiencing the same issue with watchOS 8.1 (and 8.3 beta) while it was working with previous watchOS versions.
通过在 TabView 中移动 NavigationView,我们能够让它再次工作.这种解决方法并不理想,但似乎确实有效.
We were able to get it working again by moving the NavigationView inside the TabView. This workaround isn't ideal at all but it does seem to work.
@State private var tabSelection = 1
var body: some Scene {
WindowGroup {
TabView(selection: $tabSelection) {
NavigationView {
// List goes here
}
.tag(1)
VStack(alignment: .center, spacing: 12, content: {
// content 2nd tab: we didn't have a list in the 2nd tab
})
.tag(2)
}
}
}
但是,此修复程序影响了两件事:
However, there are 2 things impacted with this fix:
我没有让 navigationBarTitle 工作,所以屏幕顶部不会有标题.如果您点击列表中的一个项目,它将导航到您的页面(如预期),但屏幕底部的 TabView 点将保留.
I didn't get the navigationBarTitle working, so there won't be a title on top of the screen.If you click on an item in the list, it will navigate to your page (as expected) but the TabView dots at the bottom of the screen will remain.
这篇关于SwiftUI:NavigationLink 在 WatchOS 8.1RC 上的 Tabview 中立即弹出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!