8

我在 watchOS 8.1RC 中发现了一个回归,其中 NavigationLink 从 TabView 触发。它立即被驳回。

它在 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()
    }
}
4

1 回答 1

0

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)
        }
    }
}

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

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

于 2021-11-12T10:44:41.550 回答