SwiftUI:当 List 和 ForEach 嵌入到 TabView 时,WatchOS 8.1 中的 NavigationView 错误 [英] SwiftUI: NavigationView bug in WatchOS 8.1 When List and ForEach is embedded in TabView

查看:23
本文介绍了SwiftUI:当 List 和 ForEach 嵌入到 TabView 时,WatchOS 8.1 中的 NavigationView 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The code below worked fine in WatchOS 7 and 8.0, but now in 8.1 tapping on the row will navigate to the destination but then immediately navigates back to the root view.

我提交了反馈 #FB9727188 并包含以下内容以证明该问题.

I filed Feedback #FB9727188 and included the below to demonstrate the issue.

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

推荐答案

我在使用 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.

通过将 NavigationView 移动到 TabView 中,我们能够让它再次工作.这种解决方法并不理想,但似乎确实有效.

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 {
                // 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:当 List 和 ForEach 嵌入到 TabView 时,WatchOS 8.1 中的 NavigationView 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆