HStack

A view that arranges its subviews in a horizontal line.

ComponentiOS 13.0+macOS 10.15+tvOS 13.0+watchOS 6.0+HStack in SwiftUI

Declaration

React
import { HStack } from 'swiftui-react-native';
Swift
@frozen nonisolated struct HStack<Content> where Content : View

Initializers

One SwiftUI initializer backs <HStack>.

React
<HStack
  alignment?: SwiftUI.VerticalAlignment
  spacing?: number | null
>
  children?: ReactNode
</HStack>
Swift
nonisolated init(
    alignment: VerticalAlignment = .center,
    spacing: CGFloat? = nil,
    @ContentBuilder content: () -> Content
)
  • The guide for aligning the subviews in this stack. This guide has the same vertical screen coordinate for every subview.

    Default `.center`

  • spacingnumber | null

    The distance between adjacent subviews, or nil if you want the stack to choose a default distance for each pair of subviews.

    Default `nil`

  • childrenReactNode

    Content. Strings become Text; React Native views are laid out by Yoga and placed by SwiftUI.

    Swift content: () -> Content

Also takes the common view props: modifiers, style and testID.

Overview

Unlike LazyHStack, which only renders the views when your app needs to display them onscreen, an HStack renders the views all at once, regardless of whether they are on- or offscreen. Use the regular HStack when you have a small number of subviews or don't want the delayed rendering behavior of the "lazy" version.

The following example shows a simple horizontal stack of five text views:

var body: some View {
    HStack(
        alignment: .top,
        spacing: 10
    ) {
        ForEach(
            1...5,
            id: \.self
        ) {
            Text("Item \($0)")
        }
    }
}

protocol, like when you want to create a conditional layout using AnyLayout, use HStackLayout instead.

From Apple’s SwiftUI documentation. Code samples there are Swift.

Relationships

Conforms to

  • View

See also

More in Layout.