Warm tip: This article is reproduced from serverfault.com, please click

uicolor-SwiftUI:使用资产目录中的颜色集

(uicolor - SwiftUI: Using Color Set from Asset Catalog)

发布于 2019-06-30 10:22:21

在SwiftUI中,我们可以使用以下方法从资产目录中的颜色集中获取颜色:

extension Color {
    static let coral = Color("coral")
}

这需要使用字符串类型的名称,并且在使用许多颜色集时会变得非常乏味。还有另一种获取颜色集的方法,类似于我们使用图像文字从资产目录中获取图像的方法吗?或者,只是少一些多余的东西。

如果没有,如何在SwiftUI中以编程方式创建动态颜色?例如,这是在UIKit中完成的方式:

extension UIColor {
    static let dynamicColor = UIColor { $0.userInterfaceStyle == .dark ? .black : .white }
}
Questioner
JWK
Viewed
11
Asperi 2020-06-26 11:36:08

如果没有,如何在SwiftUI中以编程方式创建动态颜色?例如,这是在UIKit中完成的方式:

这可以几乎相同:

extension Color {
    static let dynamicColor = Color(UIColor { traitCollection in
        return traitCollection.userInterfaceStyle == .dark ? .black : .white
    })
}