问题描述
我正在尝试将 SwiftUI Color 更改为 UIColor 的实例.
I'm trying to change a SwiftUI Color to an instance of UIColor.
我可以轻松地从 UIColor 获取 RGBA,但我不知道如何获取颜色"实例返回相应的 RGB 和不透明度值.
I can easily get the RGBA from the UIColor, but I don't know how to get the "Color" instance to return the corresponding RGB and opacity values.
@EnvironmentObject var colorStore: ColorStore
init() {
let red = //get red value from colorStore.primaryThemeColor
let green = //get green value from colorStore.primaryThemeColor
let blue = //get blue value from colorStore.primaryThemeColor
let alpha = //get alpha value from colorStore.primaryThemeColor
let color = UIColor(red: red, green: green, blue: blue, alpha: alpha)
UINavigationBar.appearance().tintColor = color
}
...或者也许有更好的方法来完成我正在寻找的东西?
...or maybe there is a better way to accomplish what I am looking for?
推荐答案
SwiftUI 2.0
有一个新的初始化器,它采用 Color
并为 iOS 或 NSColor
返回 UIColor
为 macOS 现在.所以:
SwiftUI 2.0
There is a new initializer that takes a Color
and returns a UIColor
for iOS or NSColor
for macOS now. So:
UIColor(Color.red)
macOS
NSColor(Color.red)
核心图形
UIColor(Color.red).cgColor /* For iOS */
NSColor(Color.red).cgColor /* For macOS */
如果您正在寻找颜色组件,您可以在在此答案中找到有用的扩展
If you are looking for color components, you can find a helpful extensions here in this answer
另外,请查看如何将 UIColor 转换为 SwiftUI 的颜色
Also, check out How to convert UIColor to SwiftUI‘s Color
这篇关于如何将 SwiftUI 颜色更改为 UIColor?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!