iosswiftstoryboardibinspectable

Set IBInspectable programmatically


I would like to set different IBInspectable items based on internet information I get. For example, I have IBInspectables for green, red and orange circles. If the request says green, I would like to set isGreen inspectable to Yes, and others to no. Same goes for other two. You can see IBInspectables here. I know I can do it with the code I've put in there, but is there a way to switch them programmatically?


Solution

  • Any IBInspectable, is merely a property coded to be seen in IB. So if you have this:

    @IBInspectable var isGreen:Bool? {
        didSet {
            // code to color circle?
        }
    }
    

    You can do this:

    self.isGreen = true