【问题标题】:edgesIgnoringSafeArea on TabView with PageTabViewStyle not working带有 PageTabViewStyle 的 TabView 上的edgesIgnoringSafeArea 不起作用
【发布时间】:2020-10-17 00:10:15
【问题描述】:

使用.tabViewStyle(PageTabViewStyle()) 将 TabView 用作页面查看器可以正常工作,但尝试通过应用 edgesIgnoringSafeArea 让它从边缘运行到边缘似乎不起作用。

我在这里错过了什么?

struct ContentView: View {
    let colors: [Color] = [.red, .green, .blue]
    var body: some View {
        TabView {
            ForEach(0...2, id: \.self) { index in
                Rectangle()
                    .fill(colors[index])
            }
        }
        .tabViewStyle(PageTabViewStyle())
        .edgesIgnoringSafeArea(.all)
    }
}

将另一个 .edgesIgnoringSafeArea(.all) 添加到RectangleForEach 也不起作用。

请注意,所有这些问题都是不同的,因为它们使用 use PageTabViewStyle():

他们的解决方案(添加edgesIgnoringSafeArea(.all))在这种情况下不起作用。

【问题讨论】:

  • 适用于 Xcode 12.2 beta 3

标签: ios swiftui ios14


【解决方案1】:
  1. 从 TabView 中删除 .edgesIgnoringSafeArea(.all)
  2. 将框架添加到具有屏幕宽度和高度的 TabView
  3. 用 ScrollView 包装 TabView
  4. 将 .edgesIgnoringSafeArea(.all) 添加到 ScrollView
struct ContentView: View {
    let colors: [Color] = [.red, .green, .blue]

    var body: some View {
        ScrollView {
            TabView {
                ForEach(0...2, id: \.self) { index in
                    Rectangle()
                        .fill(colors[index])
                }
            }
            .frame(
                width: UIScreen.main.bounds.width ,
                height: UIScreen.main.bounds.height
            )
            .tabViewStyle(PageTabViewStyle())
            
        }
        .edgesIgnoringSafeArea(.all)
    }
}

【讨论】:

  • 这适用于 iOS 14.3!我不喜欢使用 UIScreen.main.bounds.width/height,所以在我将 .edgesIgnoringSafeArea(.all) 移到封闭的 GeometryReader 之后,我能够获得全屏选项卡视图而忽略安全区域使用 geometry.size.width/height 设置 TabView 的框架。谢谢!!
  • @andrewcar 你能分享一下你是如何使用 GeometryReader 的吗?
  • @bze12 原理同上。只需将ScrollView 包装在GeometryReader 中,然后用几何阅读器中的宽度/高度替换框架宽度/高度。这绝对感觉像是TabViewPageTabViewStyle 中的一个错误,但至少以上是一个很好的解决方法。能够获得一个相当复杂的视图,视图的上半部分作为分页视图,而下半部分则以这种方式工作。
【解决方案2】:

这是我所拥有的最大数量...无论如何,我认为最初它是一个错误,值得向 Apple 提交反馈。

使用 Xcode 12b 测试

struct TestPagingStyle: View {
    let colors: [Color] = [.red, .green, .blue]
    var body: some View {
        ZStack {
            Color.black.overlay(
                GeometryReader { gp in
                    TabView {
                        ForEach(0..<3, id: \.self) { index in
                            Text("Hello, World \(index)")
                                .frame(width: gp.size.width, height: gp.size.height)
                                .background(colors[index])
                        }
                    }
                    .tabViewStyle(PageTabViewStyle(indexDisplayMode: .always))
                }
            )
        }.edgesIgnoringSafeArea(.all)
    }
}

【讨论】:

    【解决方案3】:

    SwiftUI 3.0 更新:

     TabView {
                ForEach(0...2, id: \.self) { index in
                    Rectangle()
                        .fill(colors[index])
                }
                .ignoresSafeArea()
            }
            .tabViewStyle(PageTabViewStyle())
            .edgesIgnoringSafeArea(.all)
    

    【讨论】:

      【解决方案4】:

      我通过“扩展”TabView 的高度并将其向顶部“移动”相同的量来解决这个烦人的问题:

      struct ContentView: View {
          var body: some View {
              let yExtension: CGFloat = 50
              GeometryReader { g in
                  TabView {
                      Color.red.overlay(Text("1"))
                      Color.green.overlay(Text("2"))
                      Color.blue.overlay(Text("3"))
                      Color.orange.overlay(Text("4"))
                  }
                  .frame(width: g.size.width, height: g.size.height + yExtension)
                  .tabViewStyle(PageTabViewStyle(indexDisplayMode: .automatic))
                  .font(Font.title.bold())
              }
              .offset(y: -yExtension)
              .edgesIgnoringSafeArea(.all)
          }
      }
      

      在 Xcode 12.4、iOS 14.4 上测试:

      【讨论】:

      • 您的解决方案比 Apple 提供的容易出错的 PageTabViewStyle 更好。但是,您的解决方案不允许旋转到横向。当您这样做时,页面索引会跳到开头(这很可能不是用户想要的)。 ScrollView-approach that kimigory showed above 在这方面要好一些。
      猜你喜欢
      • 1970-01-01
      • 2021-01-16
      • 2020-11-01
      • 1970-01-01
      • 2020-12-09
      • 1970-01-01
      • 1970-01-01
      • 2021-04-07
      • 2022-11-10
      相关资源
      最近更新 更多