3

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

Роман
  • 51
  • 1
  • 5

2 Answers2

0

Here it is

var body: some View {
    VStack(spacing: 0) { // << here !!
Asperi
  • 228,894
  • 20
  • 464
  • 690
  • 4
    I believe the question was for "ZStack" Not "VStack" – avatarZuko May 20 '21 at 04:48
  • @avatarZuko, the question of topic starter was *how to remove weird padding* - and the reason was not in `ZStack`... how it is formulated does not matter in this case - this is a fix for provided code. – Asperi Nov 04 '21 at 10:42
-2

You have provide different width in

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

the width is 210 and other element width is 200 provide the same width the padding will remove.

Sumeet Mourya
  • 434
  • 1
  • 7
  • 23