简体   繁体   中英

Get Value from SwiftUI Color Picker

iOS 14 shipped with native color pickers for UIKit ( UIColorWell ), AppKit ( NSColorWell ) and SwiftUI ( ColorPicker ).

While the color wells have a property selectedColor that exposes the selected UIColor / NSColor values, the SwiftUI ColorPicker takes a Binding<Color> . However, I can't find any information how to get anything useful from the Color value.

Creating a Color from a UIColor is easy using Color.init(_: UIColor) but there does not seem to be a way to get a UIColor from an existing Color or to extract the color components (eg the RGB values similar to the UIColor.getRed(_:green:blue:alpha:) method).

How do I get the underlying color values from a SwiftUI ColorPicker ?

In majority of cases you use Color in SwiftUI directly, ie it is not needed to extract anything from it.

Anyway if it is needed to have UIColor SwiftUI 2.0 provides new API for that

 extension UIColor { @available(iOS 14.0, tvOS 14.0, watchOS 7.0, *) @available(OSX, unavailable) public convenience init(_ color: Color) }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM