I'm facing trouble switching colours when change mode in iOS 13.
The problem is when you see the app in recent apps. UIColors will change but cgColor will not change.
It will change effectively when you open the app but just in recent it will not change.
I'm using "traitCollectionDidChange" method as mentioned in following apple's document. https://developer.apple.com/documentation/xcode/supporting_dark_mode_in_your_interface
Please have a look at Video to understand more clearly 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)
}
}
For cgColor you need to use