How to Align UI Elements Horizontally in iOS SwiftUI
SwiftUI provides various UI elements to make iOS development easy. Let’s learn how to align items horizontally in a layout using SwiftUI.
Stacks are views that help you to align items vertically, horizontally, and in the z-direction. The HStack accepts children elements and arranges them horizontally.
Read- how to align items vertically in iOS app using SwiftUI.
Create a new iOS app project using Xcode. Open ContentView.swift and replace the existing code with the following one.
import SwiftUI
struct ContentView: View {
var body: some View {
HStack{
Text("Text 1")
Text("Text 2")
Text("Text 3")
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Here, three texts are passed as children to HStack so that they will be aligned horizontally. See the output given below.
That’s how you align items in horizontal direction in SwiftUI.