Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to adjust spacing between HStack elements in swiftUI?

I have added spacer(minLength: 5) but it takes the minlenght 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

like image 837
Let's_Create Avatar asked Jun 08 '19 12:06

Let's_Create


People also ask

What is HStack default spacing?

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

What is VStack in Swift?

VStack allows to arrange its child views in a vertical line, and ZStack allows to overlap its child views on top of each other. Stacks can further be customized with alignment and spacing in order to modify their appearance.

How do I start VStack at top?

If you want all your items to be at the top, just put Spacer() as the last item in VStack, and it will push all items to the top. Show activity on this post.

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.


2 Answers

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")         }     } } 
like image 94
Marius Waldal Avatar answered Oct 11 '22 15:10

Marius Waldal


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")         }     } } 
like image 31
PinkeshGjr Avatar answered Oct 11 '22 17:10

PinkeshGjr