0

I want to implement Dark mode in a huge project and i don't want to create outlets for each labels and views in order to change the colour as you know it's difficult.

i have created two colour choosers for UIView Subclass as an example using @IBInspectable.

-dayModeBGColor -nightModeBGColor

But App crashes.

My code is as shown below.(Please don't suggest any other way)

import UIKit



class ThemeView: UIView {




    @IBInspectable var DarkColor: UIColor = UIColor.black {

        didSet{
            self.layer.backgroundColor = (self.DarkColor as! CGColor)
        }

    }


    @IBInspectable var DayColor: UIColor = UIColor.white {

        didSet{
            self.layer.backgroundColor = (self.DayColor as! CGColor)
        }

    }


    var selectedBGColor : UIColor = UIColor.white




    override func awakeFromNib() {


        if Theme.shared.isDarkMode == true {

            self.layer.backgroundColor = (DarkColor as! CGColor)

        }
        else{
            self.layer.backgroundColor = (DayColor as! CGColor)
        }


    }

}
May Rest in Peace
  • 2,070
  • 2
  • 20
  • 34
Jaseel.Dev
  • 730
  • 1
  • 7
  • 19
  • Change (DayColor as! CGColor) for DayColor.cgColor – 108g Aug 06 '19 at 09:26
  • 1
    Possible duplicate of [Converting UIColor to CGColor in swift](https://stackoverflow.com/questions/27821785/converting-uicolor-to-cgcolor-in-swift) – Gereon Aug 06 '19 at 09:35

1 Answers1

2

The app is probably crashing because of the force-casting of UIColor to CGColor.

The correct way should be self.layer.backgroundColor = self.DarkColor.cgColor

May Rest in Peace
  • 2,070
  • 2
  • 20
  • 34