iOS 13暗黑模式下切换cgColor的问题

3

我在iOS 13中切换模式时遇到了颜色切换的问题。

问题出现在你在最近使用的应用中看到应用程序时。UIColor会改变,但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)
    }
}
5个回答

11

对于cgColor,你需要使用

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

感谢您的回复,但遗憾的是这对我没有起作用。正如您在问题中所看到的视频,这个问题不是因为您的应用程序在前台运行,而是当您的应用程序在后台运行并且更改模式时,打开最近查看,您会发现属于UIView和UIViewController的颜色会改变,但CALayer的颜色却错误地改变了。 - undefined

3
当您更改模式时,会调用traitCollectionDidChange

class YourView: UIView {

  override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
    super.traitCollectionDidChange(previousTraitCollection)

    self.layer.shadowColor = yourUIColor.cgColor
  }

}

1
顺便说一句,根据你正在子类化的内容,你可能想要添加一个 "super.traitCollectionDidChange(…)" :) - undefined

2
低级别的类,如CA Layer和CG Color,并不理解动态颜色。这是UIKit的一个概念。
参考:https://developer.apple.com/videos/play/wwdc2019/214/?time=1415 对于UIView, 我花了很多时间来弄清楚;因此,我用以下方法解决了这个问题。
    
    override open func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
        super.traitCollectionDidChange(previousTraitCollection)
        guard traitCollection.hasDifferentColorAppearance(comparedTo: previousTraitCollection) else { return }
        customizeUpdateLayers()
    }
    func customizeUpdateLayers() {
        // do sth
    }


对于NSView,将该代码移至您的视图的updateLayer()方法中...
参考:https://developer.apple.com/documentation/uikit/appearance_customization/supporting_dark_mode_in_your_interface#2993898 参考:func updateLayer

0

我遇到了同样的问题。我通过移除子图层,重新创建并添加这个新的图层,并设置所需的颜色来解决它。在iOS13和iOS14上都有效。


-5

在这里,你可以用一行代码解决你所有的应用程序问题。

只需将这行代码放入你的 appDelegate 文件中即可。

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

或者,你可以像这样在info.plist中添加

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

1
Khushal,我正在寻找解决这个问题的方法。你提出的答案是禁用暗模式支持,但对我来说不适用。非常感谢你的时间,但这不是我想知道的。 - undefined
此解决方案不支持CGColor,正确答案是Jawad提供的。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接