SwiftUI:NavigationLink 在 WatchOS 8.1RC 的 Tabview 中立即弹出

Rap*_*euf 10 apple-watch swiftui swiftui-navigationlink swiftui-tabview watchos-8

我发现 watchOS 8.1RC 中存在从 TabView 触发的 NavigationLink 的回归。马上就被驳回了。

它在 watchOS 8.0 或模拟器 (watchOS 8.0) 中运行。你知道解决方法吗?谢谢

示例代码:

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()
    }
}
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(alignment: .center, spacing: 12, content: {
                
                // content 2nd tab: we didn't have a list in the 2nd tab
            })
            .tag(2)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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