How it works
ListSectionSpacing describes how much vertical space a List places between its sections, letting you tune the rhythm of a sectioned list without resorting to custom insets or spacer rows. By default a list uses the platform's standard section gap; reach for ListSectionSpacing when that gap feels too loose or too tight for your layout — for example to pull grouped rows closer together in a dense settings-style screen. You apply it through the listSectionSpacing(_:) modifier, passing either a named spacing value or an explicit point value.
Apply spacing with listSectionSpacing(_:)
The
listSectionSpacing(_:)modifier is where the value takes effect. Attach it to aList(or to an individualSection) and it sets the space inserted between that list's sections. In the example it hangs off the wholeList, so the gap between theInboxandArchivesections is what changes.Choose a semantic value: compact, default, or custom
ListSectionSpacingexposes named values that adapt to the platform rather than hard-coding a number.ListSectionSpacing.compactrequests the tightest standard gap, whileListSectionSpacing.defaultrestores the system's normal spacing. The example usesListSectionSpacing.compactto draw the two sections close together.Pass a precise gap with the point-value form
When a named value isn't exact enough,
listSectionSpacing(_:)also accepts aCGFloat, applying that many points between sections —listSectionSpacing(0)removes the gap entirely. This is the escape hatch from the semanticListSectionSpacing.compactused here when you need a specific measurement.Understand the scope it governs
The value only affects the spacing *between* sections, not the padding inside a
Sectionor between individual rows. The twoSectionblocks —Inboxwith itsTextrows andArchivewith its own — keep their internal layout; only the seam between them responds to theListSectionSpacingvalue.
.listSectionSpacing(ListSectionSpacing.compact) to .listSectionSpacing(ListSectionSpacing.default) (or to a literal like .listSectionSpacing(40)) and watch the gap between the Inbox and Archive sections grow.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 ListSectionSpacingDemo: View {
var body: some View {
List {
Section("Inbox") {
Text("Mom")
Text("Alex")
}
Section("Archive") {
Text("Newsletter")
Text("Receipts")
}
}
.listSectionSpacing(ListSectionSpacing.compact)
}
}