0 Replies
      Latest reply on Feb 28, 2020 3:43 PM by Vivek.a
      Vivek.a Level 1 Level 1 (0 points)

        I'm facing trouble switching colours when change mode in iOS 13.

         

        The problem is when you see the app in "recent apps view" (basically when you suspend your app). UIColors will change but cgColor will not change.

        It will change effectively when you open the app (when your app is in foreground).

         

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