Color Picker in SwiftUI (iOS and macOS)

Color Picker in SwiftUI (iOS and macOS)

Color is a fun aspect of app development. By adding a little bit of color, your app gains life, looks better and improves the user’s experience.

Many times the colors are hardcoded and cannot be changed, but sometimes you want to let the user choose. For example in drawing apps the user can choose the color of the pen. In a note taking app they might be able to choose the color of a note or text.

For these cases you can use the ColorPicker from SwiftUI. This view lets the user pick any color they want using a beautiful UI that’s readily available for developers to add to their app.

Color Picker

It’s very easy to add the color picker in your code.

The view takes only 2 parameters:

  • title — text that will be displayed on the color picker button and the top of the color picker dialog
  • color — a binding that will contain the selected color
struct ColorPickerExample: View {
	@State private var color: Color = .blue
	
	var body: some View {
		VStack {
			ColorPicker(
				"Choose the background color",
				selection: $color
			)
		}
		.padding()
		.frame(maxWidth: .infinity, maxHeight: .infinity)
		.background(color)
	}
}

0:00
/0:07

SwiftUI ColorPicker on iOS showing color selection and live background change.

0:00
/0:11

SwiftUI ColorPicker on macOS showing color selection and live background change.

Opacity support

Fading out guy (opacity decreases)

By default the color picker supports opacity (how transparent a color is). If you don’t want this, you can disable it by setting the supportsOpacity parameter to false.

ColorPicker(
	"Choose the background color",
	selection: $color,
	supportsOpacity: false
)
iOS ColorPicker showing opacity slider enabled, allowing transparent color selection in SwiftUI.
iOS: Color picker with opacity
iOS ColorPicker without opacity control in SwiftUI, showing only fully opaque color options.
iOS: Color picker without opacity
macOS ColorPicker in SwiftUI with opacity control visible, allowing adjustable transparency.
macOS: Color picker with opacity
macOS ColorPicker in SwiftUI without opacity setting, showing solid color selection interface.
macOS: Color picker without opacity

Disabling supportsOpacity removes the opacity control, ensuring users can only select fully opaque colors.

Hide label

Homer Simpson hides in bush (like how ColorPicker label hides)

You can also hide the title of the picker button and show only the icon. To do this, apply the .labelsHidden() modifier to the view.

ColorPicker(
	"Choose the background color",
	selection: $color
)
.labelsHidden()
SwiftUI ColorPicker on iOS using labelsHidden modifier to hide the text label and show only the color icon.
iOS: Color picker with hidden label
SwiftUI ColorPicker on macOS with labelsHidden applied, displaying icon-only button without text label.
macOS: Color picker with hidden label

Subtitles

Sometimes you want a more detailed description. To clarify the button’s role, you can add subtitles up to 3 levels, by adding multiple Text views inside the color picker’s label builder.

ColorPicker(selection: $color) {
	Text("Choose the background color")  // Title
	Text("Choose any color")  // Subtitle (Level 1)
	Text("This will affect the app's background")  // Subtitle (Level 2)
	Text("Warning: the text might be hard to read")  // Subtitle (Level 3)
}
SwiftUI ColorPicker on iOS showing multiple text levels with title and three subtitles for detailed labeling.
iOS: Color picker with multiple subtitles
SwiftUI ColorPicker on macOS displaying hierarchical text levels: main title and three subtitle lines in smaller font.
macOS: Color picker with multiple subtitles

The first text will be the title. The following ones are subtitles, each one a level below the previous. There are 3 subtitle levels. Even if you add more, SwiftUI would handle the last ones as level 3 subtitles (the smallest size).

You don’t need to add 3 subtitles. Add only as many as you see fit. This example demonstrates all 3 subtitle levels and their appearance.

Show picker programatically (not covered here)

The SwiftUI ColorPicker offers a simple way to allow users to select colors in your app.

It has some limitations though. For example it doesn’t support showing the picker programatically or creating a fully custom button to present the picker. The only solution is to create a wrapper around UIColorPickerViewController from UIKit.

The drawback is that since it’s a wrapper you need to write more code and might not be as stable as using the native SwiftUI view.

How to see real colors

To see real colors, look at nature.
Touch grass. Go outside and see real colors in nature, not just your computer.

Thanks for reading, byeee!