5
votes

I am trying to have two views in an HStack using SwiftUI. However, I keep getting a gap between the two views. The problem occurs in both portrait and landscape layouts.

my code:

struct ContentView: View {

    var body: some View {

        GeometryReader { g in
            HStack {
                GeometryReader { g in
                    HStack {
                        VStack {
                            Text("View One")
                        }
                        .frame(width: g.size.width * 0.5, height: g.size.height, alignment: .center)
                        .background(Color.blue)

                        VStack {
                            Text("View Two")
                        }
                        .frame(width: g.size.width * 0.5, height: g.size.height, alignment: .center)
                        .background(Color.red)
                    }
                }
            }         
        }    
    }
}

The gap is between the View One and View Two

1
Try setting the spacing by using HStack(spacing: ) - Malik
Have you tried to add spacing, like so: HStack(spacing: 0)? EDIT: @Malik you were faster ;-) - Jeroen
Hmm, what happens if you try to make the size (width) of both VStacks slightly smaller? - Jeroen
It worked with (Spacing: 0) put the answer so I can accept it - Fady E
@Malik post the answer so I can accept it - Fady E

1 Answers

13
votes

The spacing can be setup at the time of initialising

HStack(spacing: 0) {
    //Your code here
}