我目前正在采用“暗模式”,我发现在Interface Builder中使用systemBackground
和label
这样的新系统颜色在 iOS12。我一半希望得到编译器错误,但该应用程序看起来像是在iOS 13轻模式下。因此很明显,运行时以某种方式为iOS 12转换了这些颜色。
有人知道引擎盖下会发生什么,是否有方便的方法可以在代码中实现相同的目的?
答案 0 :(得分:7)
如果查看情节提要的XML,您将看到类似以下内容的
:<color key="backgroundColor" xcode11CocoaTouchSystemColor="systemBackgroundColor" cocoaTouchSystemColor="whiteColor"/>
Xcode 11增加了对两种颜色的支持。无论哪种机制,只要在运行时将.storyboard文件转换为操作,它都具有所需的信息,该信息需要知道iOS 13使用哪种颜色以及iOS 12及更低版本使用哪种颜色。
在代码中,您需要以下内容:
extension UIColor {
class var mySystemBackground: UIColor {
if #available(iOS 13, *) {
return .systemBackground
} else {
return .white
}
}
}