Simple swift color picker popover (iOS)

color-palette, color-picker, ios, popover, swift

Solution

With iOS 14 Apple has now implemented a standard UIColorPickerViewController and associated UIColorWell that is a color swatch that automatically brings up the UIColorPicker to choose a color.

You can test the ColorPicker by creating a Swift App project with Xcode 12 or later, targeting iOS 14+ and then try this simple code:

import SwiftUI

struct ContentView: View {
    @State private var bgColor = Color.white

    var body: some View {
        VStack {
            ColorPicker("Set the background color",
                        selection: $bgColor,
                        supportsOpacity: true)
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .background(bgColor)
    }
}

Change `supportsOpacity` to `false` to get rid of the opacity slider and only allow fully opaque colors.

ColorPicker showing two different modes of selection:

ColorPicker without alpha:

Problem

Is there is a simple way to implement a color picker popover in swift? Are there any built-in libraries or UI elements that I could leverage for this purpose? I saw some color pickers written in objective-c, but they were several years old and I was wondering if there was something more recent.

Original source