TechnologiesSwiftUI

ColumnsFormStyle struct

iOSmacOStvOSwatchOSvisionOSiOS 16.0+✓ renders

A non-scrolling form style with a trailing aligned column of labels

How it works

ColumnsFormStyle is the form style that arranges a form's controls into aligned columns, placing each row's label in one column and its editable value in another. It conforms to FormStyle, the protocol SwiftUI uses to lay out the contents of a Form, and it's the layout you get when you ask for the column-aligned presentation typical of settings windows on macOS. Reach for it when you want labels and fields to line up neatly side by side rather than stacking, so a dense group of inputs reads as a tidy table of name-and-value pairs.

  1. Apply it with the .columns form style

    You rarely name ColumnsFormStyle directly; instead you select it through the static FormStyle accessor .columns, which resolves to an instance of this type. In the example, .formStyle(.columns) is what swaps the form over to column-aligned layout.

  2. Attach it to a Form with formStyle(_:)

    ColumnsFormStyle only takes effect once it's handed to the formStyle(_:) view modifier, which installs a FormStyle into the environment for the enclosing Form to read. Here Form { ... }.formStyle(.columns) binds the style to that specific form and its Section.

  3. Let it align labels and values into columns

    The style's job is to split each labeled control into a leading label column and a trailing value column, keeping them aligned across rows. The labels "Name", "Notifications", and "Volume" settle into one column while the TextField, Toggle, and Slider editors line up in the next.

  4. Rely on its FormStyle conformance for layout decisions

    Because ColumnsFormStyle conforms to FormStyle, SwiftUI routes the form's body through the style's makeBody(configuration:) to produce the columnar arrangement; you supply the content, the style supplies the structure. The whole Section("Profile") and its controls are laid out by this conformance rather than by hand.

Try it — Change .formStyle(.columns) to .formStyle(.grouped) and watch the labels move from beside their fields to above them, revealing exactly what the column alignment of ColumnsFormStyle was doing.

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.

ColumnsFormStyle.swift
struct ColumnsFormStyleDemo: View {
    @State private var name = "Ada"
    @State private var notifications = true
    @State private var volume = 0.6

    var body: some View {
        Form {
            Section("Profile") {
                TextField("Name", text: $name)
                Toggle("Notifications", isOn: $notifications)
                Slider(value: $volume) {
                    Text("Volume")
                }
            }
        }
        .formStyle(.columns)
        .padding()
    }
}
Live preview
Profile Ada Notifications
Profile Ada Notifications
swift → lexer → parser → sema → uiir → canvas Open in Studio ↗
What's new in SwiftUI 27 →