UIColorPickerViewController opens, but does not work and appears incorrect
// swiftui
@State private var showDialog = false
@State private var color: Color = .white
// some code swiftui
Button("Title") {
showDialog = true
}
.sheet(isPresented: $showDialog) {
ColorPickerViewRepresentable(color: $color)
}
// uikit
struct ColorPickerViewRepresentable: UIViewControllerRepresentable {
@Binding var color: Color
func makeUIViewController(context: Context) -> UIColorPickerViewController {
let vc = UIColorPickerViewController()
vc.delegate = context.coordinator
vc.selectedColor = UIColor(color)
vc.supportsAlpha = false
return vc
}
func updateUIViewController(_ uiViewController: UIColorPickerViewController, context: Context) {
uiViewController.selectedColor.resolvedColor(with: UITraitCollection.current)
}
func makeCoordinator() -> Coordinator {
return Coordinator(parent: self)
}
class Coordinator: NSObject, UIColorPickerViewControllerDelegate {
var parent: ColorPickerViewRepresentable
init(parent: ColorPickerViewRepresentable) {
self.parent = parent
}
func colorPickerViewControllerDidFinish(_ viewController: UIColorPickerViewController) {
parent.color = Color(uiColor: viewController.selectedColor)
}
}
}