Alignment
An alignment in both axes.
Declaration
type Alignment =
| 'bottom'
| 'bottomLeading'
| 'bottomTrailing'
| 'center'
| 'centerFirstTextBaseline'
| 'centerLastTextBaseline'
| 'leading'
| 'leadingFirstTextBaseline'
| 'leadingLastTextBaseline'
| 'top'
| 'topLeading'
| 'topTrailing'
| 'trailing'
| 'trailingFirstTextBaseline'
| 'trailingLastTextBaseline'
| { horizontal: HorizontalAlignment; vertical: VerticalAlignment; };@frozen struct AlignmentPass a literal, or build one with SwiftUI.Alignment from import { SwiftUI } from 'swiftui-react-native'.
Type properties
'bottom'SwiftUI.Alignment.bottomA guide that marks the bottom edge of the view.
'bottomLeading'SwiftUI.Alignment.bottomLeadingA guide that marks the bottom and leading edges of the view.
'bottomTrailing'SwiftUI.Alignment.bottomTrailingA guide that marks the bottom and trailing edges of the view.
'center'SwiftUI.Alignment.centerA guide that marks the center of the view.
'centerFirstTextBaseline'SwiftUI.Alignment.centerFirstTextBaselineA guide that marks the top-most text baseline in a view.
'centerLastTextBaseline'SwiftUI.Alignment.centerLastTextBaselineA guide that marks the bottom-most text baseline in a view.
'leading'SwiftUI.Alignment.leadingA guide that marks the leading edge of the view.
'leadingFirstTextBaseline'SwiftUI.Alignment.leadingFirstTextBaselineA guide that marks the leading edge and top-most text baseline in a view.
'leadingLastTextBaseline'SwiftUI.Alignment.leadingLastTextBaselineA guide that marks the leading edge and bottom-most text baseline in a view.
'top'SwiftUI.Alignment.topA guide that marks the top edge of the view.
'topLeading'SwiftUI.Alignment.topLeadingA guide that marks the top and leading edges of the view.
'topTrailing'SwiftUI.Alignment.topTrailingA guide that marks the top and trailing edges of the view.
'trailing'SwiftUI.Alignment.trailingA guide that marks the trailing edge of the view.
'trailingFirstTextBaseline'SwiftUI.Alignment.trailingFirstTextBaselineA guide that marks the trailing edge and top-most text baseline in a view.
'trailingLastTextBaseline'SwiftUI.Alignment.trailingLastTextBaselineA guide that marks the trailing edge and bottom-most text baseline in a view.
Overview
An Alignment contains a HorizontalAlignment guide and a
VerticalAlignment guide. Specify an alignment to direct the behavior of
certain layout containers and modifiers, like when you place views in a
ZStack, or layer a view in front of or behind another view using
View.overlay(alignment:content:) or
View.background(alignment:content:), respectively. During layout,
SwiftUI brings the specified guides of the affected views together,
aligning the views.
SwiftUI provides a set of built-in alignments that represent common combinations of the built-in horizontal and vertical alignment guides. The blue boxes in the following diagram demonstrate the alignment named by each box's label, relative to the background view:
The following code generates the diagram above, where each blue box appears in an overlay that's configured with a different alignment:
struct AlignmentGallery: View {
var body: some View {
BackgroundView()
.overlay(alignment: .topLeading) { box(".topLeading") }
.overlay(alignment: .top) { box(".top") }
.overlay(alignment: .topTrailing) { box(".topTrailing") }
.overlay(alignment: .leading) { box(".leading") }
.overlay(alignment: .center) { box(".center") }
.overlay(alignment: .trailing) { box(".trailing") }
.overlay(alignment: .bottomLeading) { box(".bottomLeading") }
.overlay(alignment: .bottom) { box(".bottom") }
.overlay(alignment: .bottomTrailing) { box(".bottomTrailing") }
.overlay(alignment: .leadingLastTextBaseline) { box(".leadingLastTextBaseline") }
.overlay(alignment: .trailingFirstTextBaseline) { box(".trailingFirstTextBaseline") }
}
private func box(_ name: String) -> some View {
Text(name)
.font(.system(.caption, design: .monospaced))
.padding(2)
.foregroundColor(.white)
.background(.blue.opacity(0.8), in: Rectangle())
}
}
private struct BackgroundView: View {
var body: some View {
Grid(horizontalSpacing: 0, verticalSpacing: 0) {
GridRow {
Text("Some text in an upper quadrant")
Color.gray.opacity(0.3)
}
GridRow {
Color.gray.opacity(0.3)
Text("More text in a lower quadrant")
}
}
.aspectRatio(1, contentMode: .fit)
.foregroundColor(.secondary)
.border(.gray)
}
}To avoid crowding, the alignment diagram shows only two of the available
text baseline alignments. The others align as their names imply. Notice that
the first text baseline alignment aligns with the top-most line of text in
the background view, while the last text baseline aligns with the
bottom-most line. For more information about text baseline alignment, see
VerticalAlignment.
In a left-to-right language like English, the leading and trailing
alignments appear on the left and right edges, respectively. SwiftUI
reverses these in right-to-left language environments. For more
information, see HorizontalAlignment.
Custom alignment
You can create custom alignments --- which you typically do to make use
of custom horizontal or vertical guides --- by using the
init(horizontal:vertical:) initializer. For example, you can combine
a custom vertical guide called firstThird with the built-in horizontal
HorizontalAlignment.center guide, and use it to configure a ZStack:
ZStack(alignment: Alignment(horizontal: .center, vertical: .firstThird)) {
// ...
}For more information about creating custom guides, including the code
that creates the custom firstThird alignment in the example above,
see AlignmentID.
From Apple’s SwiftUI documentation. Code samples there are Swift.
Used by
- GridA container view that arranges other views in a two dimensional layout.
- GridLayoutA grid that you can use in conditional layouts.
- ZStackA view that overlays its subviews, aligning them in both axes.
- ZStackLayoutAn overlaying container that you can use in conditional layouts.
- backgroundSets the view's background to a style.
- backgroundPreferenceValueReads the specified preference value from the view, using it to produce a second view that is applied as the background of the original view.
- containerBackgroundSets the container background of the enclosing container using a view.
- containerRelativeFramePositions this view within an invisible frame with a size relative to the nearest container.
- contentMarginsConfigures the content margin for a provided placement.
- framePositions this view within an invisible frame.
- ignoresSafeAreaExpands the safe area of a view.
- maskMasks this view using the alpha channel of the given view.
- overlayLayers the specified style in front of this view.
- overlayPreferenceValueReads the specified preference value from the view, using it to produce a second view that is applied as an overlay to the original view.
- presentationBackgroundSets the presentation background of the enclosing sheet using a shape style.
See also
More in Layout.
- ContainerBackgroundPlacementThe placement of a container background.
- ContainerValuesReadableKeys of SwiftUI ContainerValues JavaScript can read.
- ContainerValuesWritableKeys of SwiftUI ContainerValues JavaScript can write.
- ControlSizeThe size classes, like regular or small, that you can apply to controls within a view.
- DocumentLaunchGeometryProxyA proxy for access to the frame of the scene and its title view.
- DynamicTypeSizeA Dynamic Type size, which specifies how large scalable content should be.
- EdgeAn enumeration to indicate one edge of a rectangle.
- EdgeInsetsThe inset distances for the sides of a rectangle.
- EdgeInsetsChainEdgeInsets with chained calls: SwiftUI.EdgeInsets.inset(...).
- EdgeSetAn efficient set of edges.
- GeometryProxyA proxy for access to the size and coordinate space (for anchor resolution) of the container view.
- GridItemA description of a row or a column in a lazy grid.