TechnologiesSwiftUI

Divider struct

iOSmacOStvOSwatchOSvisionOSiOS 13.0+✓ renders

A visual element that can be used to separate other content.

How it works

A Divider is a lightweight visual separator that draws a thin line between adjacent elements, helping you group related content and signal where one section ends and another begins. Reach for it when a layout needs structure but a heavier container like a box or a card would be too much. In a stack it sizes and orients itself automatically, so you place it where you want a break rather than configuring a shape by hand.

  1. Create the separator with Divider()

    Divider is a View you instantiate with its no-argument initializer, Divider(). It carries no content of its own; it simply renders a hairline rule wherever you drop it into a view hierarchy, as it does twice between the Text rows here.

  2. Let the enclosing stack set its orientation

    Divider reads the axis of the layout it lives in: inside a vertical container it draws a horizontal line spanning the available width, and inside a horizontal one it draws a vertical line. Because it sits in a VStack, each Divider() stretches across the column to split "Account", "Settings", and "Sign Out" into distinct bands.

  3. Combine it with the stack's spacing

    A Divider participates in normal stack layout, so the container's own spacing applies on both sides of the line. The spacing: 12 on the VStack gives every Divider() breathing room above and below, turning a flat list of Text views into clearly separated groups.

  4. Apply view modifiers like any other view

    Because Divider conforms to View, you can attach standard modifiers to tune its appearance or footprint. Wrapping it in .padding(), .frame(...), or .foregroundColor(...) adjusts its inset, length, or tint the same way those modifiers affect the surrounding Text elements in this layout.

Try it — Swap the outer VStack(spacing: 12) for an HStack(spacing: 12) and watch each Divider() flip to a vertical line that separates the Text items side by side.

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.

Divider.swift
struct DividerDemo: View {
    var body: some View {
        VStack(spacing: 12) {
            Text("Account")
                .font(.headline)
            Divider()
            Text("Settings")
            Divider()
            Text("Sign Out")
                .foregroundColor(.red)
        }
        .padding()
    }
}
Live preview
Account Settings Sign Out
Account Settings Sign Out
swift → lexer → parser → sema → uiir → canvas Open in Studio ↗
What's new in SwiftUI 27 →