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?
Asked
Active
Viewed 2,425 times
2 Answers
6
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
-
So, in my ViewController, I should add @IBInspectable isGreen and in viewWillAppear what do I write? – Dragos Strugar Dec 03 '16 at 13:42
-
Where should I determine which of them to call, and how to trigger them? – Dragos Strugar Dec 03 '16 at 13:43
-
I misread your question. You probably could refactor things, as IBInspectable properties already have knowledge of theUIColor type. [link]http://nshipster.com/ibinspectable-ibdesignable/ shows a good example of this. – Dec 03 '16 at 13:51
-
Yeah. thanks. Very good resource. Solved my problem. You have to type all the code that changes the UI in the didSet {} – Dragos Strugar Dec 03 '16 at 13:56
0
You sound confused. The term IBInspectable
describes a property of a view object that has the @IBInspectable
tag on it. That tells Interface Builder that you want to be able to change that property from inside Interface Builder.
Aside from being editable from Interface Builder, an IBInspectable property is no different than any other property.
If you have a class CircleView, with a property diameter:
class FooView: UIView {
@IBInspectable var diameter: CGFloat {
didSet {
//Code to do something with a change to the diameter property
}
}
And your view controller has a FooView object in it with an outlet, then you can change the value of your custom property in code just like any other property:
class MyViewController: UIViewController {
@IBOulet weak var fooView: FooView!
@IBAction func buttonTapped(sender: UIButton) {
fooView.diameter += 5
}
}

Duncan C
- 128,072
- 22
- 173
- 272