Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gap in HStack between two views in SwiftUI

Tags:

swift

swiftui

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

like image 291
Fady E Avatar asked Oct 25 '19 06:10

Fady E


People also ask

How many views can HStack SwiftUI have?

HStack can contain up to 10 static views, if you need more static views, you can nest HStack inside another HStack or Group to nest views inside.

What is HStack and VStack in SwiftUI?

Individually, HStack , VStack , and ZStack are simple views. HStack positions views in a horizontal line, VStack positions them in a vertical line, and ZStack overlays views on top of one another.

How do I set margins in SwiftUI?

You need to use . padding modifier for margin. In your code, you have to add padding inside your ScrollView. After that, inside BoxViewTable, you need to add .


1 Answers

The spacing can be setup at the time of initialising

HStack(spacing: 0) {
    //Your code here
}
like image 176
Malik Avatar answered Nov 07 '22 00:11

Malik