TechnologiesSwiftUI

Section struct

iOSmacOStvOSwatchOSvisionOSiOS 13.0+✓ renders

A container view that you can use to add hierarchy within certain views.

How it works

A Section groups a set of related views into a visually and semantically distinct block, most often inside a List or a Form. By dividing a long collection of rows into labeled groups, it gives the reader structure — a separated band of content with its own header, and optionally a footer. Reach for Section whenever a single flat list would be hard to scan and the rows naturally fall into categories, such as the settings groups shown here.

  1. Wrap related rows in a Section

    Section is a container view: you pass a trailing closure whose contents become the grouped rows, and SwiftUI renders them as one cohesive block inside the enclosing container. Here two Section values sit directly inside the List, each holding a pair of Text rows like Text("Profile") and Text("Privacy").

  2. Give the group a header with the title initializer

    The init(_:content:) initializer takes a string title that becomes the section's header, letting you name the group without building a separate header view. The example passes "Account" and "Notifications" as those titles, so each block of rows is announced by a heading above it.

  3. Place sections inside a List or Form

    Section derives its styling — inset grouping, separators, header treatment — from the container it lives in, so it is meant to be nested rather than used standalone. In this code both sections are children of the List, which is what turns the plain title strings into styled group headers and visually separates Account from Notifications.

  4. Reach for the header/footer initializer when you need both

    Beyond the title form, Section offers initializers that take explicit header and footer view builders for richer captions or trailing explanatory text. The example uses only the string-title overload, but swapping to that form would let each group carry a footer beneath rows such as Text("Email") and Text("Push").

Try it — Add a third row like Text("Sounds") inside the Section("Notifications") closure and watch it join that group under the same header rather than starting a new band.

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.

Section.swift
struct SectionDemo: View {
    var body: some View {
        List {
            Section("Account") {
                Text("Profile")
                Text("Privacy")
            }
            Section("Notifications") {
                Text("Email")
                Text("Push")
            }
        }
    }
}
Live preview
Account Profile Privacy Notifications Email Push
Account Profile Privacy Notifications Email Push
swift → lexer → parser → sema → uiir → canvas Open in Studio ↗
What's new in SwiftUI 27 →