Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove padding bewteen items in HStack

Tags:

xcode

swiftui

I'm trying to define a sub-view which is three buttons, each inside a rounded rectangle. The first button is a fixed width, the other two buttons should share the remaining width. This is what I'm getting: enter image description here

You can see that there is some padding between the buttons, and although the two larger buttons are the correct size, the padding makes the whole thing too wide.

Here is my SwiftUI code:

struct TopBarView: View {
var body: some View {
    GeometryReader { geometry in
        HStack {
            ZStack {
                RoundedRectangle(cornerRadius: 8, style: .continuous )
                    .stroke()
                    
                Button(action: sideMenu) {
                    Image(systemName: "line.horizontal.3")
                }
            }.frame(width: 48, height: 48, alignment: .center)
            
            ZStack {
                RoundedRectangle(cornerRadius: 8, style: .continuous)
                    .stroke()
                
                Button(action: sideMenu) {
                    Text( "First")
                }
            }.frame(width: (geometry.size.width - 48)/2, height: 48)
            
            ZStack {
                RoundedRectangle(cornerRadius: 8, style: .continuous)
                    .stroke()
    
                Button(action: sideMenu) {
                    Text( "Second")
                }
            }.frame(width: (geometry.size.width - 48)/2, height: 48)
        }
    }
}

func sideMenu() -> Void {
    
}

}

So my question is - whats the correct way to remove the gap beween the buttons so that everything fits?

like image 811
Kenny Avatar asked Dec 30 '25 12:12

Kenny


1 Answers

You can pass the spacing parameter to HStack to remove the spacing like this:

HStack(spacing: 0) { 
    //some content
}
like image 126
the.blaggy Avatar answered Jan 01 '26 17:01

the.blaggy



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!