Is there is a simple way to implement a color-picker popover in Swift? Are there any built-in UI elements that I could leverage for this purpose?
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: