应用“框架”修改器时的ScrollView布局错误

时间:2020-03-18 15:58:23

标签: ios swiftui

我偶然发现了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”在滚动视图框架之外。那是正常的吗?有什么办法可以解决?

enter image description here

删除frame(...)修饰符时,一切正常。 enter image description here

2 个答案:

答案 0 :(得分:0)

这是一种解决方法(在某些情况下,可以作为临时解决方案来接受)

通过Xcode 11.2 / iOS 13.2测试

demo

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

答案 1 :(得分:0)

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

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

enter image description here