How it works
TableColumnAlignment describes how a column's content sits within its horizontal extent in a SwiftUI Table — leading, center, or trailing. Because a table places every cell in a column inside the same fixed-width region, the alignment you choose governs where the value lands and where its header label is drawn relative to it. Reach for it when a column's values read better pinned to one edge — most commonly numeric or measurement columns, which are conventionally aligned to the trailing edge so that digits line up vertically down the rows.
Pass an alignment to TableColumn
A
TableColumnAlignmentvalue is supplied through thealignment:parameter of aTableColumn. It applies to that one column's cells and its header together, leaving sibling columns untouched. In the example,TableColumn("Qty", alignment: .trailing)requests trailing alignment for the quantity column while the"Name"column keeps the default.Choose among the alignment cases
TableColumnAlignmentis a small set of type properties —leading,center, andtrailing— each producing a value of the type. You select one with leading-dot syntax at the call site, as with.trailinghere. Omitting thealignment:argument entirely, as the"Name"column does, falls back to the column's natural leading layout.Header and cells share the alignment
The chosen
TableColumnAlignmentpositions both the column's header text and the content view each row builds for that column. So.trailingpushes the"Qty"label and everyText("\($0.qty)")cell to the right edge of the column, giving the numbers a clean right margin that the default leading layout would not.Apply it where it matters across columns
Mixing alignments per column is the point: text-bearing columns typically stay leading while numeric ones go trailing. The demo pairs a default-aligned
TableColumn("Name")with a.trailingTableColumn("Qty"), so names flush left and counts flush right within the sameTable(items).
alignment: .trailing on the "Qty" column to alignment: .center and watch both the header and the numbers shift to the middle of the column.Example & preview
Press Run live & edit to compile it in your browser — then edit the Swift on the left and the preview re-renders live.
struct TableColumnAlignmentDemo: View {
struct Item: Identifiable {
let id = UUID()
let name: String
let qty: Int
}
let items = [
Item(name: "Apples", qty: 4),
Item(name: "Pears", qty: 12),
Item(name: "Figs", qty: 7)
]
var body: some View {
Table(items) {
TableColumn("Name") { Text($0.name) }
TableColumn("Qty", alignment: .trailing) {
Text("\($0.qty)")
}
}
.padding()
}
}