SwiftUI:当 List 和 ForEach 嵌入 TabView 时,WatchOS 8.1 中的 NavigationView 错误

Gar*_*abo 6 apple-watch swiftui swiftui-navigationlink watchos-8

下面的代码在 WatchOS 7 和 8.0 中运行良好,但现在在 8.1 中,点击该行将导航到目的地,但随后立即导航回根视图。

我提交了反馈#FB9727188,并包含以下内容来演示该问题。

struct ContentView: View {

 @State var tabIndex:Int = 0

    var body: some View {
            TabView(selection: $tabIndex) {
            ListView()
                .tabItem { Group{
                    Text("List")
                }}.tag(0)
                .padding(.bottom, 1.0)
             Text("Second View")
                .tabItem { Group{
                    Text("Second")
                }}.tag(1)
            .padding(.bottom, 1.0)
             Text("Third View")
                .tabItem { Group{
                    Text("ThirdView")
                }}.tag(2)
           
        
            
        }
    }
}


struct ListView: View {
    var body: some View {
         List {
            ForEach((1...10).reversed(), id: \.self) {_ in
                NavigationLink(destination: Text("test")) {
                    Text("Tap Me but we'll just be back here")
                }
            }
            
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

O.O*_*ier 1

我在使用 watchOS 8.1(和 8.3 beta)时遇到了同样的问题,而它与以前的 watchOS 版本一起使用。

我们能够通过移动NavigationView内部来让它再次工作TabView。这个解决方法根本不理想,但似乎确实有效。

@State private var tabSelection = 1

var body: some Scene {
    WindowGroup {
        TabView(selection: $tabSelection) {
            NavigationView {
                // List goes here
            }
            .tag(1)
            
            VStack {
                // content 2nd tab: we didn't have a list in the 2nd tab
            }
            .tag(2)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,此修复会影响两件事:

  • 我没有得到navigationBarTitle工作,所以屏幕顶部不会有标题。
  • 如果您单击列表中的某个项目,它将导航到您的页面(如预期),但屏幕底部的 TabView 点将保留。