SwiftUI HStack

Christine_Yang's avatar
Published Oct 10, 2022
Contribute to Docs

The HStack view is a layout container where elements, or subviews, are arranged in a horizontal line (left-to-right).

  • Learn how to build iOS applications with Swift and SwiftUI and publish them to Apples' App Store.
    • Includes 7 Courses
    • With Certificate
    • Beginner Friendly.
      13 hours
  • A powerful programming language developed by Apple for iOS, macOS, and more.
    • Beginner Friendly.
      12 hours

Syntax

var body: some View {
  HStack {
    Subviews here
  }
}

The HStack view rests within the body of a View.

Example

In the example below, two Text subviews are nested in the HStack:

var body: some View {
HStack {
Text("Subview 1")
Text("Subview 2")
}
}

This will display the following:

HStack

All contributors

Contribute to Docs

Learn SwiftUI on Codecademy

  • Learn how to build iOS applications with Swift and SwiftUI and publish them to Apples' App Store.
    • Includes 7 Courses
    • With Certificate
    • Beginner Friendly.
      13 hours
  • A powerful programming language developed by Apple for iOS, macOS, and more.
    • Beginner Friendly.
      12 hours