Align
Position a child widget within its parent.
Overview
Align
positions its child at a specific location within itself. Unlike Center
, which always centers the child, Align
can position the child anywhere.
Basic Usage
// Center child (default)
Align {
Text("Centered")
}
// Top left
Align(alignment: .topLeft) {
Text("Top Left")
}
// Bottom right
Align(alignment: .bottomRight) {
Text("Bottom Right")
}
// Or use the .center() modifier
Text("Centered")
.center()
Align Examples
Alignment Options
.topLeft .topCenter .topRight
.centerLeft .center .centerRight
.bottomLeft .bottomCenter .bottomRight
Custom Alignment
Use Alignment
for precise positioning:
Align(alignment: Alignment(x: 0.5, y: -0.5)) {
Text("Custom position")
}
Coordinates:
- x: -1 (left) to 1 (right)
- y: -1 (top) to 1 (bottom)
Parameters
Parameter | Type | Description | Default |
---|---|---|---|
alignment | Alignment | Where to position child | .center |
child | Widget | The widget to align | required |