SwiftUI:NavigationView 内的 TabView 内的列表问题

发布于 2025-01-18 20:21:00 字数 2577 浏览 5 评论 0原文

我想根据所选选项卡在带有不同标题的导航视图中放置一个TABVIEW。在这些选项卡中,我想放置一个列表视图。请参阅下面的代码:

 struct ContentView: View {
 @State private var selection = 1
 
 var body: some View {
     TabView(selection:$selection) {
         Page_1()
             .tabItem {
                 Image(systemName: "book")
                 Text("Page 1")
             }
             .tag(1)
         
         Page_2()
             .tabItem {
                 Image(systemName: "calendar")
                 Text("Page 2")
             }
             .tag(2)  
     }
   }
 }


 struct Page_1: View {
    @State var selectedTab = "1"
 
    var body: some View {
        NavigationView {
            TabView(selection: $selectedTab) {
                List {
                    ForEach(0..<20){i in
                        Text("Test")
                    }
                }
                .tag("1")
                .navigationBarTitle("Page 1 Tab 1")
             
                List {
                    ForEach(0..<20){i in
                        Text("Test")
                    }
                }
                .tag("2")
                .navigationBarTitle("Page 1 Tab 2")
            }
            .tabViewStyle(.page(indexDisplayMode: .never))
            .ignoresSafeArea(.all)
            .background()
        }
     }
 }


 struct Page_2: View {
    @State var selectedTab = "1"

    var body: some View {
       NavigationView {
        
          TabView(selection: $selectedTab) {
              List {
                ForEach(0..<20){i in
                    Text("Test")
                }
              }
              .tag("1")
              .navigationBarTitle("Page 2 Tab 1")
            
              List {
                 ForEach(0..<20){i in
                    Text("Test")
                 }
              }
              .tag("2")
              .navigationBarTitle("Page 2 Tab 2")
          }
          .tabViewStyle(.page)
          .tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
          .ignoresSafeArea()
          .background()  
      }   
    }
 }

问题是,当页面首先出现时,其tabviews中的列表似乎略低,然后向上移动。您可以看到它,尤其是当您像这里这样切换选项卡时:

​非常感谢您的帮助!:)

编辑

我试图将NavigationViews放在TabView中。这解决了错误的定位。但是,在我之间来回切换之前,它根本没有显示出视图。您可以看到下图中的外观:

”在此处输入图像说明”

I want to place a TabView inside a NavigationView with different titles depending on the selected tab. Inside those tabs I want to place a List view. See the code below:

 struct ContentView: View {
 @State private var selection = 1
 
 var body: some View {
     TabView(selection:$selection) {
         Page_1()
             .tabItem {
                 Image(systemName: "book")
                 Text("Page 1")
             }
             .tag(1)
         
         Page_2()
             .tabItem {
                 Image(systemName: "calendar")
                 Text("Page 2")
             }
             .tag(2)  
     }
   }
 }


 struct Page_1: View {
    @State var selectedTab = "1"
 
    var body: some View {
        NavigationView {
            TabView(selection: $selectedTab) {
                List {
                    ForEach(0..<20){i in
                        Text("Test")
                    }
                }
                .tag("1")
                .navigationBarTitle("Page 1 Tab 1")
             
                List {
                    ForEach(0..<20){i in
                        Text("Test")
                    }
                }
                .tag("2")
                .navigationBarTitle("Page 1 Tab 2")
            }
            .tabViewStyle(.page(indexDisplayMode: .never))
            .ignoresSafeArea(.all)
            .background()
        }
     }
 }


 struct Page_2: View {
    @State var selectedTab = "1"

    var body: some View {
       NavigationView {
        
          TabView(selection: $selectedTab) {
              List {
                ForEach(0..<20){i in
                    Text("Test")
                }
              }
              .tag("1")
              .navigationBarTitle("Page 2 Tab 1")
            
              List {
                 ForEach(0..<20){i in
                    Text("Test")
                 }
              }
              .tag("2")
              .navigationBarTitle("Page 2 Tab 2")
          }
          .tabViewStyle(.page)
          .tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
          .ignoresSafeArea()
          .background()  
      }   
    }
 }

The problem is that when the Pages first appear the lists inside their TabViews seem to be placed slightly too low and then move up. You can see this especially when you switch tabs like here:

enter image description here

After switching back and forth between the tabs they are placed correctly until I freshly start the app again. Would really appreciate your help!:)

Edit

As suggested I tried to put the NavigationViews inside the TabView. That solves the problem with the wrong positioning. However, it leads to the views not being shown at all before I switch back and forth between them. You can see what that looks like in the picture below:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

您的好友蓝忘机已上羡 2025-01-25 20:21:00

这是一个奇怪的问题,您可以根据您对即时更改列表样式的要求的要求采取不同的方法,使其消失了。

示例1:

struct Page_1: View {
    @State var selectedTab = "1"
 
    var body: some View {
        NavigationView {
            TabView(selection: $selectedTab) {
                List {
                    ForEach(0..<20){i in
                        Text("Test")
                    }
                }
                .listStyle(.plain)
                .tag("1")
                .navigationBarTitle("Page 1 Tab 1")
             
                List {
                    ForEach(0..<20){i in
                        Text("Test")
                    }
                }
                .listStyle(.plain)
                .tag("2")
                .navigationBarTitle("Page 1 Tab 2")
            }
            .tabViewStyle(.page(indexDisplayMode: .never))
            .ignoresSafeArea(.all)
            .background()
        }
     }
 }

另一个可能的解决方案是将填充添加到列表中:

示例2:

struct Page_1: View {
    @State var selectedTab = "1"
 
    var body: some View {
        NavigationView {
            TabView(selection: $selectedTab) {
                List {
                    ForEach(0..<20){i in
                        Text("Test")
                    }
                }
                .padding(.top, 1)
                .tag("1")
                .navigationBarTitle("Page 1 Tab 1")
             
                List {
                    ForEach(0..<20){i in
                        Text("Test")
                    }
                }
                .padding(.top, 1)
                .tag("2")
                .navigationBarTitle("Page 1 Tab 2")
            }
            .tabViewStyle(.page(indexDisplayMode: .never))
            .ignoresSafeArea(.all)
            .background()
        }
     }
 }

作为最后的手段,您可以更改所使用的UI组件,然后继续实现相同布局直到揭示了这种未知行为的来源为止,但我想这不是想法。

That's a weird issue, you could take different approaches depending on your requirements for instantance changing the style of your list makes it go away.

Example 1:

struct Page_1: View {
    @State var selectedTab = "1"
 
    var body: some View {
        NavigationView {
            TabView(selection: $selectedTab) {
                List {
                    ForEach(0..<20){i in
                        Text("Test")
                    }
                }
                .listStyle(.plain)
                .tag("1")
                .navigationBarTitle("Page 1 Tab 1")
             
                List {
                    ForEach(0..<20){i in
                        Text("Test")
                    }
                }
                .listStyle(.plain)
                .tag("2")
                .navigationBarTitle("Page 1 Tab 2")
            }
            .tabViewStyle(.page(indexDisplayMode: .never))
            .ignoresSafeArea(.all)
            .background()
        }
     }
 }

Another possible solution is adding a padding to the list:

Example 2:

struct Page_1: View {
    @State var selectedTab = "1"
 
    var body: some View {
        NavigationView {
            TabView(selection: $selectedTab) {
                List {
                    ForEach(0..<20){i in
                        Text("Test")
                    }
                }
                .padding(.top, 1)
                .tag("1")
                .navigationBarTitle("Page 1 Tab 1")
             
                List {
                    ForEach(0..<20){i in
                        Text("Test")
                    }
                }
                .padding(.top, 1)
                .tag("2")
                .navigationBarTitle("Page 1 Tab 2")
            }
            .tabViewStyle(.page(indexDisplayMode: .never))
            .ignoresSafeArea(.all)
            .background()
        }
     }
 }

As a last resort, you could change the UI components you are using and continue achieving the same layout until the source of this unknown behavior is revealed, but I guess that's not the idea.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文