gridColumnAlignment
Overrides the default horizontal alignment of the grid column that the view appears in.
Declaration
compose().gridColumnAlignment(
guide: SwiftUI.HorizontalAlignment
)nonisolated func gridColumnAlignment(
_ guide: HorizontalAlignment
) -> some ViewParameters
The
HorizontalAlignmentguide to use for the grid column that the view appears in.
Return value
A view that uses the specified horizontal alignment, and that causes all cells in the same column of a grid to use the same alignment.
Discussion
You set a default alignment for the cells in a grid in both vertical
and horizontal dimensions when you create the grid with the
Grid.init(alignment:horizontalSpacing:verticalSpacing:content:)
initializer. However, you can use the gridColumnAlignment(_:) modifier
to override the horizontal alignment of a column within the grid. The
following example sets a grid's alignment to
Alignment.leadingFirstTextBaseline, and then sets the first column
to use HorizontalAlignment.trailing alignment:
Grid(alignment: .leadingFirstTextBaseline) {
GridRow {
Text("Regular font:")
.gridColumnAlignment(.trailing) // Align the entire first column.
Text("Helvetica 12")
Button("Select...") { }
}
GridRow {
Text("Fixed-width font:")
Text("Menlo Regular 11")
Button("Select...") { }
}
GridRow {
Color.clear
.gridCellUnsizedAxes([.vertical, .horizontal])
Toggle("Use fixed-width font for new documents", isOn: $isOn)
.gridCellColumns(2)
}
}This creates the layout of a typical macOS configuration view, with the trailing edge of the first column flush with the leading edge of the second column:
Add the modifier to only one cell in a column. The grid automatically aligns all cells in that column the same way. You get undefined behavior if you apply different alignments to different cells in the same column.
To override row alignment, see GridRow.init(alignment:content:). To
override alignment for a single cell, see View.gridCellAnchor(_:).
See also
More in Layout.
- alignmentGuideSets the view's vertical alignment.
- aspectRatioConstrains this view's dimensions to the specified aspect ratio.
- containerCornerOffsetAdjusts the view's layout to avoid the container view's corner insets for the specified edges.
- containerRelativeFramePositions this view within an invisible frame with a size relative to the nearest container.
- controlSizeSets the size for controls within this view.
- defaultScrollAnchorAssociates an anchor to control which part of the scroll view's content should be rendered by default.
- dynamicTypeSizeSets the Dynamic Type size within the view to the given value.
- fixedSizeFixes this view at its ideal size.
- flipsForRightToLeftLayoutDirectionSets whether this view mirrors its contents horizontally when the layout direction is right-to-left.
- framePositions this view within an invisible frame.
- geometryGroupIsolates the geometry (e.g. position and size) of the view from its parent view.
- gridCellAnchorSpecifies a custom alignment anchor for a view that acts as a grid cell.