Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 748
Yotzin Castrejon Avatar asked Oct 22 '25 18:10

Yotzin Castrejon


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.

like image 130
George Avatar answered Oct 24 '25 11:10

George



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!