Is there a way to eliminate outside edge of padding in SwiftUI?

Viewed 499

So I've noticed when using padding() it creates an outside space/edge outside a view frame. Is there a way to eliminate that outside edge?

Here is .padding(.top, 0) enter image description here

Here is .padding(.top, 1) the extra outside edge has appeared enter image description here

Here is .padding(.top, 10) the outside edge remains on any padding above 1px from what I see.

enter image description here

import SwiftUI

struct TestView: View {
    var body: some View {
        VStack {
            Text("")
                .frame(width: 300, height: 20)
                .background(Color(.black))
                .cornerRadius(10)
            Text("")
                .frame(width: 300, height: 20)
                .background(Color(.black))
                .cornerRadius(10)
                .padding(.top, 0)//here you can change the 0 to 1
        }
    }
}

enter image description here

1 Answers

The reason this happens is because the VStack automatically has its own spacing. Replace:

VStack {
    /* ... */
}

with:

VStack(spacing: 0) {
    /* ... */
}

This removes padding between each view within the VStack. I am assuming SwiftUI makes an assumption that if you want 0 padding, you want them touching, otherwise you likely want padding from within its own space within the VStack.

Related