ZStack SwiftUI中如何去除填充物[重复]。

问题描述 投票:0回答:1

当我试图将另一个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,padding会消失。

Screenshot of weird padding

ios swift swiftui padding zstack
1个回答
2
投票

在这里,它是...

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

0
投票

你提供了不同的宽度在

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

宽度是 210 和其他元素的宽度为 200 提供相同宽度的填充物将被移除。

© www.soinside.com 2019 - 2024. All rights reserved.