How to adjust spacing between HStack elements in SwiftUI?
Asked Answered
D

4

70

I have added spacer(minLength: 5) but it takes the minLength.

How can I specify the spacing between the text?

I have attached a screenshot for reference. I want to reduce the spacing between inner HStack.

HStack {
    Image("Rhea").resizable().cornerRadius(25).frame(width: 50.0, height: 50.0)
        VStack(alignment: .leading) {
            Text("How to enjoy your life without money").bold().font(.system(size: 20))
            HStack {
                Text("Lets create")
                Spacer(minLength: 5)
                Text("3K views")
                Spacer(minLength: 5)
                Text("3 hours ago")
            }
        }
    }
}

enter image description here

Duggins answered 8/6, 2019 at 12:25 Comment(4)
Wait what Rhea got this far? I love the example xDIsolda
By the way this was not intentional, question was posted about 2 year ago.Talapoin
the example perfectly fits the character and I am surprised how you wrote this example a year before jun 2020Isolda
If you are looking for the correct method to achieve true space-between behavior without hard-coded spacing regardless of the container width, I have posted a detailed answer in another thread here: https://mcmap.net/q/174641/-hstack-fill-whole-width-with-equal-spacingPulmonic
H
98

Add a spacing attribute to the HStack itself. For a spacing of e.g. 10:

HStack {
    Image("Rhea").resizable().cornerRadius(25).frame(width: 50.0, height: 50.0)
    VStack(alignment: .leading) {
        Text("How to enjoy your life without money").bold().font(.system(size: 20))
        HStack(spacing: 10) {
            Text("Lets create")
            Text("3K views")
            Text("3 hours ago")
        }
    }
}
Helpmeet answered 8/6, 2019 at 12:28 Comment(0)
H
12

You can add spacing inside your SwiftUI stacks by providing a value in the initialiser, like this:

VStack

VStack(spacing: 50) {
    Text("SwiftUI")
    Text("rocks")
}

HStack

HStack(spacing: 50) {
    Text("SwiftUI")
    Text("rocks")
}

In you case you can use like below.

HStack {
Image("Rhea").resizable().cornerRadius(25).frame(width: 50.0, height: 50.0)
    VStack(alignment: .leading) {
        Text("How to enjoy your life without money").bold().font(.system(size: 20))
        HStack(spacing: 10) {
            Text("Lets create")
            Text("3K views")
            Text("3 hours ago")
        }
    }
}
Hypsometry answered 8/6, 2019 at 12:37 Comment(0)
J
8

For more flexibility there is also .padding(...):

HStack(spacing: 0) {
  Text("Lets create")
    .padding(.bottom, 10)
  Text("3K views")
    .padding(.bottom, 10)
  Text("3 hours ago")
}

Keep in mind that currently HStacks default spacing is 10, if you dont specify any or set it to nil.

Jeunesse answered 3/3, 2022 at 6:29 Comment(0)
C
1
//alignment is optional
   HStack(alignment: .center, spacing: 20) {
        // Add your views here
        Text("View 1")
            .frame(width: 100, height: 100)
            .background(Color.blue)
        
        Text("View 2")
            .frame(width: 100, height: 100)
            .background(Color.red)
        
        Text("View 3")
            .frame(width: 100, height: 100)
            .background(Color.green)
    }
Chintzy answered 26/6, 2023 at 7:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.