【问题标题】:SwiftUI Alignment BottomSwiftUI 对齐底部
【发布时间】:2020-07-16 19:54:05
【问题描述】:

我有以下代码:

struct CircleView: View {
    var body: some View {
        ZStack(alignment: .bottom) {
            VStack{
                Spacer()
                Circle().frame(width: UIScreen.main.bounds.width/5)
            }
        }
    }
}

为什么不对齐底部的圆圈?我有一个应该占用 ZStack 中所有可用空间的垫片?

提前致谢。

【问题讨论】:

    标签: swift alignment swiftui


    【解决方案1】:

    因为 Circle 是一个 Shape 并且没有自己的内容大小,所以会消耗所有提供的内容。您限制了宽度,但没有限制高度,因此 Circle 占用了所有高度(蓝色矩形全是圆圈,但仅在合适的地方淹没)。

    所以如果你想让它对齐,你必须完全限制它,如下所示

    ZStack(alignment: .bottom) {
        VStack{
            Spacer()
            Circle()
                .frame(width: UIScreen.main.bounds.width/5, height: UIScreen.main.bounds.width/5)
        }
    }
    

    【讨论】:

    • 非常感谢。没有想过限制高度。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-30
    • 1970-01-01
    • 2016-12-03
    • 2012-07-02
    • 1970-01-01
    • 2017-02-16
    • 2022-08-23
    相关资源
    最近更新 更多