ios - 在 iOS 13 深色模式下问题 cgColor 切换
问题描述
在 iOS 13 中更改模式时,我在切换颜色时遇到了麻烦。
问题是当您在最近的应用程序中看到该应用程序时。UIColors 会改变,但 cgColor 不会改变。
当你打开应用程序时它会有效地改变,但最近它不会改变。
我正在使用以下苹果文档中提到的“traitCollectionDidChange”方法。 https://developer.apple.com/documentation/xcode/supporting_dark_mode_in_your_interface
请看视频以更清楚地了解 https://www.youtube.com/watch?v=C-pcVuPMQ9U&feature=youtu.be
class ViewController: UIViewController {
@IBOutlet weak var viewColor: UIView!
let layerColor = CALayer()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
setLayer()
setColors()
}
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
if traitCollection.hasDifferentColorAppearance(comparedTo: previousTraitCollection) {
setColors()
}
}
func setColors() {
layerColor.backgroundColor = UIColor(named: "layerColor")?.cgColor
}
func setLayer() {
layerColor.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
viewColor.layer.addSublayer(layerColor)
}
}
解决方案
对于 cgColor 你需要使用
layerColor.backgroundColor = UIColor(named: "layerColor")?.resolvedColor(with: self.traitCollection).cgColor
推荐阅读
- json - 如何从带有json的长混合字符串中仅获取json数据,我只需要获取具有值的json数据
- python - 从 WebSocketApp 中的 on_message 检索数据
- git - 编辑用特殊字符填充的 .git/config 文件
- visual-studio-code - 有没有办法在主屏幕(空白屏幕)上禁用 vscod 徽标和快捷方式提示?
- google-apps-script - TypeError:无法读取 null main 的属性“getRange”
- python - xlsx 文件中的 datetime.datetime 类型阻止了进程
- database - 根据同一张表中的值获取sql整体
- node.js - 将时间事件与 Node.js 和 Wit.ai 一起用于 Facebook Messenger?
- python - AttributeError:“str”对象没有属性“option_chain”
- java - 如何使用 ISO-8859-6 字符集将十六进制代码编码为字符串?