TechnologiesSwiftUI

CompactDatePickerStyle struct

iOSmacOStvOSwatchOSvisionOS✓ renders

A date picker style that displays the components in a compact, textual

How it works

CompactDatePickerStyle is the date-picker style that renders a DatePicker as a small, tappable field showing the current value, expanding to a calendar or wheel popover only when the user activates it. It exists for space-constrained layouts — forms, settings rows, and inline controls — where a fully expanded graphical calendar would be too heavy. Reach for it when you want a date control that reads like an ordinary field at rest and reveals its full editing surface on demand. You apply it through the datePickerStyle(_:) modifier rather than constructing it directly.

  1. Apply the style with datePickerStyle(.compact)

    CompactDatePickerStyle conforms to DatePickerStyle, so you opt into it by passing the .compact shorthand to the datePickerStyle(_:) modifier on a DatePicker. In the example, .datePickerStyle(.compact) is chained onto the DatePicker and collapses it into a compact field instead of the default presentation.

  2. Drive it from a Date binding

    The style only governs presentation; the value still flows through the DatePicker's selection binding. Here selection: $date ties the compact field to the @State private var date = Date() source of truth, so tapping the field and choosing a new date writes straight back into that state.

  3. Label the compact field

    The compact control pairs a title with its inline value display, and that title comes from the DatePicker's label argument. The "Event Date" string passed to DatePicker is shown beside the tappable value, giving the field meaning when it sits in a row of other controls.

  4. Place it where space is tight

    Because CompactDatePickerStyle keeps the resting footprint to a single field, it slots cleanly into dense layouts. The .padding() around the styled DatePicker is just breathing room — the compact style itself is what keeps the control from expanding into a full calendar until it's tapped.

Try it — Swap .datePickerStyle(.compact) for .datePickerStyle(.graphical) and watch the same DatePicker expand from a tappable field into a full inline calendar.

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.

CompactDatePickerStyle.swift
struct CompactDatePickerStyleDemo: View {
    @State private var date = Date()
    var body: some View {
        DatePicker("Event Date", selection: $date)
            .datePickerStyle(.compact)
            .padding()
    }
}
Live preview
Event Date April 1, 2026
Event Date April 1, 2026
swift → lexer → parser → sema → uiir → canvas Open in Studio ↗
What's new in SwiftUI 27 →