TechnologiesSwiftUI

ColorPicker struct

iOSmacOStvOSwatchOSvisionOS✓ renders

A control used to select a color from the system color picker UI.

How it works

A ColorPicker is a control that lets people select a color from the system color interface, presenting a swatch that opens the platform's standard color panel or grid. Use it whenever your interface exposes a user-adjustable color — a stroke, a fill, a theme accent — and you want that selection to ride on a Color binding rather than a hand-built palette. Because it reads and writes a binding, the chosen color flows straight back into your view's state, keeping the picker and whatever it tints in sync.

  1. Bind the selection to a Color with the title initializer

    The common initializer takes a localized title string and a selection binding of type Binding<Color>. The title labels the control; the binding is the live channel through which the picker reports the chosen color. Here ColorPicker("Stroke Color", selection: $strokeColor) labels the row "Stroke Color" and reads and writes the $strokeColor binding.

  2. Back the binding with @State

    ColorPicker needs a mutable source of truth to mutate, so the bound Color is typically owned by a @State property. Declaring @State private var strokeColor = Color.blue gives the picker a starting value and a place to store each new selection; the leading $ in $strokeColor projects that state into the Binding<Color> the initializer expects.

  3. Drive other views from the same binding

    Because the selection writes back through the binding, any view that reads the same property re-renders when the color changes. Passing strokeColor to .fill(strokeColor) on the RoundedRectangle means the rectangle repaints the instant someone picks a new color in the picker, with no extra wiring.

  4. Reach for the opacity-aware or label-closure overloads

    Beyond the title-string form, ColorPicker offers a supportsOpacity parameter to let people adjust alpha, and a trailing-closure overload that takes an arbitrary label view instead of a string. The example uses the simplest title form, but you can swap in those initializers when you need transparency control or a custom label.

Try it — Change ColorPicker("Stroke Color", selection: $strokeColor) to ColorPicker("Stroke Color", selection: $strokeColor, supportsOpacity: true) and watch the picker gain an opacity slider whose alpha shows through the rectangle's fill.

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.

ColorPicker.swift
struct ColorPickerDemo: View {
    @State private var strokeColor = Color.blue
    var body: some View {
        VStack(spacing: 20) {
            ColorPicker("Stroke Color", selection: $strokeColor)
            RoundedRectangle(cornerRadius: 12)
                .fill(strokeColor)
                .frame(width: 120, height: 80)
        }
        .padding()
    }
}
Live preview
Stroke Color
Stroke Color
swift → lexer → parser → sema → uiir → canvas Open in Studio ↗
What's new in SwiftUI 27 →