Search code examples
iosswiftswiftuipaddingzstack

How to remove padding in the ZStack SwiftUI


I'm getting weird padding when trying to put another VStack in ZStack. How to remove it?

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

}

if I comment VStack with Text("2"), padding will disappear.

Screenshot of weird padding


Solution

  • Here it is

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