SwiftUI ColorPicker does not dismiss upon eyedropper button press

The ColorPicker sheet should dismiss when the user presses the eyedropper so that they can pick a color from the screen behind it. I made my own coordinator, so is there anyone to specifically code for when the eyedropper is pressed? Currently my sheet only dismisses half a second after a color is selected.

Code Block
import Foundation
import SwiftUI
struct ColorPicker: UIViewControllerRepresentable {
    @Environment(\.presentationMode) var presentationMode
    @Binding var colors: UIColor
    func makeCoordinator() -> Coordinator {
        Coordinator(color1: self)
    }
    
    func makeUIViewController(context: Context) -> UIColorPickerViewController {
        let colorPicker = UIColorPickerViewController()
        colorPicker.delegate = context.coordinator
        colorPicker.selectedColor = colors
        return colorPicker
    }
    func updateUIViewController(_ uiViewController: UIColorPickerViewController, context: UIViewControllerRepresentableContext<ColorPicker>) {
        uiViewController.selectedColor.resolvedColor(with: UITraitCollection.current)
    }
    class Coordinator: NSObject, UINavigationControllerDelegate, UIColorPickerViewControllerDelegate {
        var colorPick: ColorPicker
        init(color1: ColorPicker){
            colorPick = color1
        }
        
        func colorPickerViewControllerDidSelectColor(_ viewController: UIColorPickerViewController) {
            self.colorPick.colors =  viewController.selectedColor
            do{
                usleep(500000)
                viewController.dismiss(animated: true, completion: nil)
            }
        }
        func colorPickerViewControllerDidFinish(_ viewController: UIColorPickerViewController) {
        }
    }
}

I've encountered the same problem, and use UIColorWell as a work around.

Code Block
struct ColorPickerWellView: UIViewRepresentable {
private var selectedColor: Color
let onColorPicked: (UIColor) -> Void
init(selectedColor: Color, onColorPicked: @escaping (UIColor) -> Void) {
self.selectedColor = selectedColor
self.onColorPicked = onColorPicked
}
func makeUIView(context: Context) -> UIView {
let colorWell = UIColorWell(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
colorWell.title = "Select Color"
colorWell.selectedColor = UIColor(selectedColor)
colorWell.addTarget(context.coordinator, action: #selector(context.coordinator.colorWellChanged), for: .valueChanged)
return colorWell
}
func updateUIView(_: UIView, context _: Context) {}
func makeCoordinator() -> Coordinator {
Coordinator(
onColorPicked: self.onColorPicked
)
}
class Coordinator: NSObject {
private let onColorPicked: (UIColor) -> Void
init(onColorPicked: @escaping (UIColor) -> Void) {
self.onColorPicked = onColorPicked
}
@objc func colorWellChanged(_ sender: UIColorWell) {
if let c = sender.selectedColor {
self.onColorPicked(c)
}
}
}
}

Code Block swift
struct ColorPickerWellView: UIViewRepresentable {
let selectedColor: UInt32
let onColorPicked: (UIColor) -> Void
init(selectedColor: UInt32, onColorPicked: @escaping (UIColor) -> Void) {
self.selectedColor = selectedColor
self.onColorPicked = onColorPicked
}
func makeUIView(context: Context) -> UIColorWell {
let colorWell = UIColorWell(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
colorWell.title = "Select Color"
colorWell.selectedColor = UIColor(selectedColor.toColor())
colorWell.supportsAlpha = true
colorWell.addTarget(context.coordinator, action: #selector(context.coordinator.colorWellChanged), for: .valueChanged)
return colorWell
}
func updateUIView(_ colorWell: UIColorWell, context _: Context) {
colorWell.selectedColor = UIColor(selectedColor.toColor())
}
func makeCoordinator() -> Coordinator {
Coordinator(
onColorPicked: self.onColorPicked
)
}
class Coordinator: NSObject {
private let onColorPicked: (UIColor) -> Void
init(onColorPicked: @escaping (UIColor) -> Void) {
self.onColorPicked = onColorPicked
}
@objc func colorWellChanged(_ sender: UIColorWell) {
if let c = sender.selectedColor {
print(c)
self.onColorPicked(c)
}
}
}
}

updated
SwiftUI ColorPicker does not dismiss upon eyedropper button press
 
 
Q