跳转至
Structure

HorizontalAlignment

An alignment position along the horizontal axis.

Declaration

@frozen struct HorizontalAlignment : Equatable

Overview

Use this structure in the initializers of 3 structures:

See those pages for more on how to construct them.

This structure is also used in the alignmentGuide(_:computeValue:) view modifier. See that page for more on adding alignment guides to views.

There are 3 possible horizontal alignments:

  1. leading
  2. center
  3. trailing

See the example below for all three.

Leading

struct ContentView: View {
    var body: some View {
        VStack(alignment: HorizontalAlignment.leading) { //Try changing this
            Text("⬅️")

            HStack {
                Text("Leading")
                Text("Center")
                Text("Trailing")
            }
        }
        .font(.largeTitle)
    }
}

Center

struct ContentView: View {
    var body: some View {
        VStack(alignment: HorizontalAlignment.center) { //Try changing this
            Text("🤠")

            HStack {
                Text("Leading")
                Text("Center")
                Text("Trailing")
            }
        }
        .font(.largeTitle)
    }
}

Trailing

struct ContentView: View {
    var body: some View {
        VStack(alignment: HorizontalAlignment.trailing) { //Try changing this
            Text("➡️")

            HStack {
                Text("Leading")
                Text("Center")
                Text("Trailing")
            }
        }
        .font(.largeTitle)
    }
}

Availability

iOS 13.0+

macOS 10.15+

tvOS 13.0+

watchOS 6.0+

Topics


Type Property

center A guide marking the horizontal center of the view.

leading A guide marking the leading edge of the view.

trailing A guide marking the trailing edge of the view.


Initializer

init(_:) Creates an instance with the given identifier.


Type Method

==(a:b:) Returns a Boolean value indicating whether two values are equal.