HStack is a layout container used to layout child views in horizontal direction. Child views appear side by side from leading to trailing direction next to each other.
The Basics
Let’s start with a simple example. We will create HStack and add two child Text views.
struct HStackExample: View {
var body: some View {
HStack {
Text("DevTechie")
.font(.largeTitle)
Text("Learn by example")
.font(.title3)
}
}
}
Similar to VStack , HStack also takes only the space needed for it to display the content.
struct HStackExample1: View {
var body: some View {
HStack {
Text("DevTechie")
.font(.largeTitle)
Text("Learn by example")
.font(.title3)
}
.padding().border(Color.orange)
}
}
Nesting of HStacks
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.
Spacing between all child views can be changed with the help of spacing parameter. HStack takes spacing as initparameter and can be used as shown in the example below. We will also throw in a little animation for spacing as well in combination with newly introduced task modifier
HStack uses VerticalAlignment to align its child views. By default child views are center aligned. Other options are top, center, bottom, firstTextBaseline, and lastTextBaseline
Since HStack takes only as much space as needed, in order for us to see alignment change properly, we will have to make one text larger then other.
We will create alignmentProp of type VerticalAlignment so we can control alignment with the tap on a button. We will also be adding animation modifier to make alignment transition smoother.
Notice: use of firstTextAlignment and lastTextAlignment. firstTextAlignment will align bottom of “SwiftUI, Swift, iOS” text to the bottom of first text which is “DevTechie”. lastTextAlignment will align bottom of “SwiftUI, Swift, iOS” text to the bottom of last text, which is “Learn by example.”
LayoutPriority
Child views inside HStack have same priority so the space is distributed by parent accordingly. If child view requires different priority, that can be indicated by the use of layoutPriority modifier.
Let’s start with example where views are distributed equal width inside HStack:
We can use layoutPriority to make sure content is displayed properly. For our example, I will take four Text views to see layoutPriority in action:
struct HStackExample1: View {
var body: some View {
HStack {
Text("DevTechie")
.font(.largeTitle)
Text("Learn by example")
Text("SwiftUI, Swift, iOS")
Text("Without priority")
}
.frame(maxHeight: 200)
}
}
Notice how we have text wrap itself to grow in vertical direction to make room for all the other text views. Let’s say, we wanna make sure that DevTechie and Without priority text should always appear in single line, we can achieve that with layoutPriority:
struct HStackExample1: View {
var body: some View {
HStack {
Text("DevTechie")
.font(.largeTitle)
.layoutPriority(2)
Text("Learn by example")
Text("SwiftUI, Swift, iOS")
Text("Without priority")
.layoutPriority(2)
}
.frame(maxHeight: 200)
}
}
With that, we have reached the end of this article. Thank you once again for reading, if you liked it, don’t forget to subscribe our newsletter.