In SwiftUI, how does putting controls inside a Form with Sections change how they look on iOS, and how do you override that?
answer
- the container chooses the style
- grouped list on iOS
- header, footer, LabeledContent
- style modifiers win locally
basics
~20 sForm applies platform styling to the controls inside it: on iOS it is a grouped list where each Toggle, Picker, DatePicker and Button becomes a row. Override per control or subtree with pickerStyle, toggleStyle, datePickerStyle, formStyle and controlSize.
solid answer
~40 s`Form` is a container for data-entry controls that restyles what it holds. On iOS it renders as a grouped list: `Section` gives each group a header and footer, a `Toggle` becomes a row with a switch, a `Picker` a row showing its current value, a `DatePicker` a row with a compact date you tap, and a `Button` a tappable row. On macOS the same form is an aligned vertical stack, so one declaration adapts. `LabeledContent` (iOS 16) gives a read-only value or a custom control the same label-and-value row. When the default is wrong, override locally: `pickerStyle(.segmented)` or `.navigationLink`, `toggleStyle(_:)`, `datePickerStyle(.graphical)`, `formStyle(_:)` for the form itself, and `controlSize(_:)` for size. Each applies to its subtree.
code
swift · 38 linesimport SwiftUI
enum Theme: String, CaseIterable, Identifiable {
case system, light, dark
var id: Self { self }
}
struct PodcastSettings: View {
@State private var autoplay = true
@State private var theme: Theme = .system
@State private var accent = Color.orange
let storageUsed: Measurement<UnitInformationStorage>
var body: some View {
NavigationStack {
Form {
Section("Playback") {
Toggle("Autoplay next episode", isOn: $autoplay)
Picker("Theme", selection: $theme) {
ForEach(Theme.allCases) { Text($0.rawValue.capitalized) }
}
.pickerStyle(.segmented)
}
Section {
ColorPicker("Accent colour", selection: $accent, supportsOpacity: false)
LabeledContent("Storage used",
value: storageUsed,
format: .measurement(width: .abbreviated))
} header: {
Text("Library")
} footer: {
Text("Downloads are removed after you finish an episode.")
}
}
.navigationTitle("Settings")
}
}
}go deeper
Recall that Form groups controls into a list of rows on iOS and that Section adds headers and footers.
Explain how the container picks each control's style, how LabeledContent aligns custom rows, and how style modifiers propagate down a subtree.
Show judgement about which picker and date picker styles suit each setting, and why fighting the form's styling breaks other platforms.
Weigh standard Form styling against brand-specific custom styles, given the cost of keeping custom controls native on every platform.
## Form is a styling context, not just a container A **`Form`** groups controls used for data entry, such as a settings screen. Apple's documentation says SwiftUI applies **platform-appropriate styling** to the views inside a form, and that the styling reaches buttons, toggles, labels, lists and more. The concrete result differs by platform: forms appear as **grouped lists on iOS** and as **aligned vertical stacks on macOS**. That is the point of using `Form` instead of a `VStack` — the same declaration produces a native-looking settings screen on each platform. ## What each control becomes on iOS | Control | In a `VStack` | In a `Form` on iOS | |---|---|---| | `Toggle` | label and switch on one line | a list row with the label and a trailing switch | | `Picker` | depends on context | a row with the label and the current value; tap it to choose | | `DatePicker` | compact date text | a row with the label and a compact date to tap | | `Button` | a borderless text button | a tappable row in the tint colour | | `LabeledContent` | label and value side by side | a row aligned like the other controls' labels | `Section` organises the rows. Use `Section("Playback") { ... }` for a simple header, or `Section { ... } header: { ... } footer: { ... }` when you need a footer — the place for explanatory text and validation messages. The older `Section(header:content:)` initialisers are deprecated. ## LabeledContent fills the gaps **`LabeledContent`** (iOS 16) attaches a label to a value-bearing view and lays it out consistently with other controls in its container. Two uses come up on settings screens: - **Read-only values:** `LabeledContent("Storage used", value: storageUsed, format: .measurement(width: .abbreviated))` shows a formatted value; the documentation says SwiftUI makes such text selectable where it can. - **Custom controls:** `LabeledContent("Accent") { MySwatchRow(...) }` lines a hand-built control up with the built-in ones. It also changes nested styling on some platforms: the documentation's example shows toggles inside a `LabeledContent` on macOS drawn as checkboxes rather than switches. ## Overriding the defaults Every style modifier applies to the view it wraps **and everything inside it**, so you can set a rule for a whole section and still change one control. 1. **`pickerStyle(_:)`** — `.automatic`, `.menu` (iOS 14), `.segmented`, `.inline` (iOS 14), `.wheel`, `.navigationLink` (iOS 16), `.palette` (iOS 17). `.navigationLink` pushes a separate list of choices, so the form must sit inside a navigation stack. 2. **`toggleStyle(_:)`** — `.automatic`, `.switch`, `.button` (iOS 15); `.checkbox` is macOS-only. 3. **`datePickerStyle(_:)`** — `.compact`, `.graphical` (both iOS 14), `.wheel`. 4. **`formStyle(_:)`** (iOS 16) — `.automatic`, `.grouped` or `.columns` for the form as a whole. 5. **`controlSize(_:)`** — `.mini`, `.small`, `.regular`, `.large`, and `.extraLarge` from iOS 17; it sets the size for controls in the subtree through the environment. 6. **`labelsHidden()`** — hides the visual labels while keeping them for accessibility. ## Practical consequences - **Do not fight the form.** Re-creating switches and rows by hand inside a `Form` loses the platform look and makes macOS or iPad builds look wrong; reach for a style modifier first. - **A segmented picker suits two to four short options;** a menu or navigation-link picker suits long lists such as time zones. - **Choose by what the user edits:** a graphical date picker takes a lot of vertical space in a form row, so keep the compact style unless picking the date is the point of the screen. - **Use the footer for help and errors**, so messages sit under the group they describe. - **Custom styles** (your own `ToggleStyle` or `ButtonStyle` types) are a separate subject; built-in styles cover most settings screens. ## Reading a form as a reviewer When reviewing a settings screen, check three things in order: 1. Is every control inside a `Section` that names its group, with help text in a footer rather than in loose `Text` rows? 2. Does each picker's style suit its option count and the space it has? 3. Are hand-built rows wrapped in `LabeledContent` so they align with the built-in ones? ## Summary The container decides the default look, `Section` decides grouping, `LabeledContent` aligns anything that is not a built-in control, and style modifiers override locally. A candidate who can predict how a `Toggle` or `Picker` will render inside a `Form` before running the app understands that SwiftUI controls describe *what* they edit and let the context decide *how* they look.
- Why does a Picker with .navigationLink style do nothing in a Form that is not inside a NavigationStack?The navigation-link style presents its choices by pushing a new list onto a navigation stack. With no stack above the form there is nothing to push onto, so the row cannot show its choices. Wrap the screen in a `NavigationStack`, or use `.menu` or `.inline` when the form is shown without one.
- When would you use LabeledContent instead of an HStack with a Text and a Spacer?Whenever the row lives in a form or another container with its own label layout. `LabeledContent` adapts to the container, aligns with built-in controls' labels on each platform, and makes text values selectable where possible, while a hand-built `HStack` looks right on one platform only.
- How do you stop a ColorPicker in a settings Form from offering transparency?Pass `supportsOpacity: false`, as in `ColorPicker("Accent colour", selection: $accent, supportsOpacity: false)`. The documentation says it then hides the opacity controls and strips opacity from colours set programmatically or chosen from the user's favourites. The default is `true`.
- A Form is just a VStack with padding
- Each Toggle in a Form needs a custom style to look native
- Section(header:content:) is the current way to add a header
- Style modifiers only affect the one view they are attached to
- A navigationLink picker works without a navigation stack