【问题标题】:ScrollView layout bug when applying Frame modifier应用 Frame 修饰符时出现 ScrollView 布局错误
【发布时间】:2020-03-18 15:58:23
【问题描述】:

我偶然发现了 SwiftUI 中 ScrollView 的布局问题。 我认为这是一个 SwiftU 错误,但我想与您再次确认。

这是我的简单视图代码:

struct ContentView: View {
    var body: some View {
        ScrollView(.vertical) {
            Text("Hello, World!")
                .background(Color.red)
        }
        .frame(width: 300, height: 300)
        .background(Color.blue)
    }
}

这就是它的外观。您可以看到“Hello World”在滚动视图框架之外。这正常吗?有什么办法可以解决吗?

当我删除 frame(...) 修饰符时,一切正常。

【问题讨论】:

  • 为错误投票。越早提交给 Apple 越好。
  • @Asperi 我阿吉!不幸的是,当前版本中有很多错误。 ...顺便说一句,我使用不同的解决方法(见我的回答)

标签: ios swiftui


【解决方案1】:

这里有一些解决方法(在某些情况下作为临时解决方案可能是可以接受的)

使用 Xcode 11.2 / iOS 13.2 测试

var body: some View {
    GeometryReader { gp in
        ScrollView {
            Text("Hello, World!")
                .background(Color.red)
        }
        .frame(maxWidth: 300, maxHeight: 300)
        .background(Color.blue)
    }
}

【讨论】:

    【解决方案2】:

    有一个可怕的错误,我建议下一个解决方法

    struct ContentView: View {
        var body: some View {
            ScrollView(.vertical, showsIndicators: true) {
                Text("Hello, World!")
                    .background(Color.red)
            }
            // workaround, please report the bug to Apple !!
            .offset(x: 0, y: 0.5)
            .background(Color.yellow)
            .frame(width: 200, height: 200)
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多