ColorPicker

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

ComponentiOS 14.0+macOS 11.0+ColorPicker in SwiftUI

Declaration

React
import { ColorPicker } from 'swiftui-react-native';
Swift
nonisolated struct ColorPicker<Label> where Label : View

Initializers

2 SwiftUI initializers back <ColorPicker>. The props you pass pick one.

React
<ColorPicker
  selection: Binding<SwiftUI.Color>
  supportsOpacity?: boolean
>
  children?: ReactNode
</ColorPicker>
Swift
nonisolated init(
    selection: Binding<Color>,
    supportsOpacity: Bool = true,
    @ContentBuilder label: () -> Label
)
  • selection*Binding<SwiftUI.Color>

    A Binding to the variable that displays the selected [Color](/docs/components/color).

  • supportsOpacityboolean

    A Boolean value that indicates whether the color picker allows adjusting the selected color's opacity; the default is true.

    Default `true`

  • childrenReactNode

    Content. Strings become Text; React Native views are laid out by Yoga and placed by SwiftUI.

    Swift label: () -> Label

Also takes the common view props: modifiers, style and testID.

Overview

The color picker shows the currently selected color and displays the larger system color picker that allows people to select a new color.

By default color picker supports colors with opacity; to disable opacity support, set the supportsOpacity parameter to false. In this mode the color picker won't show controls for adjusting the opacity of the selected color, and strips out opacity from any color set programmatically or selected from the user's system favorites.

You use ColorPicker by embedding it inside a view hierarchy and initializing it with a title string and a Binding to a Color:

struct FormattingControls: View {
    @State private var bgColor =
        Color(.sRGB, red: 0.98, green: 0.9, blue: 0.2)

    var body: some View {
        VStack {
            ColorPicker("Alignment Guides", selection: $bgColor)
        }
    }
}

From Apple’s SwiftUI documentation. Code samples there are Swift.

Relationships

Conforms to

  • View

See also

More in Controls.