【发布时间】:2020-07-15 23:24:23
【问题描述】:
我们可以使用动态提供程序来扩展 UIColor 类,并让颜色立即变为深色和浅色模式,就像这样
extension UIColor {
static var myControlBackground: UIColor {
return UIColor { (traits) -> UIColor in
// Return one of two colors depending on light or dark mode
return traits.userInterfaceStyle == .dark ?
UIColor(red: 0.5, green: 0.4, blue: 0.3, alpha: 1) :
UIColor(red: 0.3, green: 0.4, blue: 0.5, alpha: 1)
}
}
}
但是 UIImages 呢?
我知道你可以用这个方法
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
if traitCollection.hasDifferentColorAppearance(comparedTo:
previousTraitCollection) {
// Color change detected.
// Adjust the interface accordingly.
}
}
问题在于,如果应用程序在发生更改时处于后台,则此方法不会检测到更改。颜色动态方法可以。
任何想法
【问题讨论】:
-
traitCollectionDidChange在外观改变时如果应用程序处于后台,则不会检测到变化。 -
我想知道我是否可以通过编程方式做到这一点,或者换句话说,不使用资产目录中的图像集。创建一种在外观发生变化时自动将图像更改为暗/亮等效物的方式。
标签: ios uiimage uicolor ios-darkmode