【问题标题】:issue cgColor switching in iOS 13 dark mode在 iOS 13 深色模式下问题 cgColor 切换
【发布时间】:2020-06-10 22:00:20
【问题描述】:

在 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)
    }
}

【问题讨论】:

    标签: ios swift calayer uibezierpath cgcolor


    【解决方案1】:

    对于 cgColor 你需要使用

    layerColor.backgroundColor = UIColor(named: "layerColor")?.resolvedColor(with: self.traitCollection).cgColor
    

    【讨论】:

    • 感谢您的回复,但遗憾的是这对我不起作用。正如您在video 中看到的那样,我已经发布了相关问题。这个问题不是你的应用程序是前台的,而是你的应用程序是后台的,你改变了模式,打开重新发送的视图,你会发现属于 UIview 和 UIViewController 的颜色会改变,但是 CALayer 的颜色改变错误。
    【解决方案2】:

    您的所有应用都在这里提供一站式解决方案,

    只要你把这一行放在你的 appDelegate 文件中

    if #available(iOS 13.0, *) {
        window?.overrideUserInterfaceStyle = .light
    }
    

    。你可以像这样添加 info.plist

    <key>UIUserInterfaceStyle</key>
    <string>Light</string>
    

    【讨论】:

    • Khushal 我正在寻求解决这个问题。您建议的答案是防止暗模式支持。这不适用于我。非常感谢您抽出宝贵时间,但这不是我想知道的。
    • 此解决方案不支持CGColor,正确答案是Jawad
    【解决方案3】:

    我遇到了同样的问题。我通过删除子层、重新创建它并添加具有所需颜色的新子层来解决它。它适用于 iOS13 和 iOS14。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      • 2020-01-30
      • 2019-11-22
      • 1970-01-01
      • 1970-01-01
      • 2023-01-04
      相关资源
      最近更新 更多