【问题标题】:How to remove padding in the ZStack SwiftUI [duplicate]如何在 ZStack SwiftUI 中删除填充 [重复]
【发布时间】:2020-08-21 01:49:42
【问题描述】:

在尝试将另一个 VStack 放入 ZStack 时,我得到了奇怪的填充。怎么去掉?

struct ContentView: View {
var body: some View {
    VStack{
        VStack{
            Text("1")
        }
        .frame(width: 200, height: 50)
        .background(Color.init(.green))

        ZStack{
            VStack{
                Text("2")
            }
            .frame(width: 210, height: 50)
            .background(Color.init(.blue))
            VStack{
                Text("3")
            }
            .frame(width: 200, height: 50)
            .background(Color.init(.green))
        }

        VStack{
            Text("4")
        }
        .frame(width: 200, height: 50)
        .background(Color.init(.green))
    }    
}

}

如果我用 Text("2") 注释 VStack,填充将消失。

【问题讨论】:

    标签: ios swift swiftui padding zstack


    【解决方案1】:

    在这里

    var body: some View {
        VStack(spacing: 0) { // << here !!
    

    【讨论】:

    • 我相信问题是针对“ZStack”而不是“VStack”
    • @avatarZuko,主题启动器的问题是如何删除奇怪的填充 - 原因不在ZStack...如何制定并不重要这种情况 - 这是对提供的代码的修复。
    【解决方案2】:

    您在

    中提供了不同的宽度
    VStack{
          Text("2")
    }
    .frame(width: 210, height: 50)
    .background(Color.init(.blue))
    

    宽度为210,其他元素宽度为200 提供与填充将移除的宽度相同的宽度。

    【讨论】:

    • 不,它不会删除。我说的是 1 和 3 / 3 和 4 之间的垂直填充
    • 好的.. 这是因为 Top VStack 视图。在此 VStack 中提供间距以避免此空间/边距/填充。
    猜你喜欢
    • 1970-01-01
    • 2020-02-16
    • 2021-03-19
    • 2020-10-11
    • 2019-10-30
    • 1970-01-01
    • 2020-06-28
    • 2021-05-23
    相关资源
    最近更新 更多