I know default values of IBInspectable-properties can be set as:
@IBInspectable var propertyName:propertyType = defaultValue
in Swift. But how do I achieve a similar effect in Objective-C so that I can have default value of some property set to something in Interface Builder?
Why don't use use the macro such as:
???
The
prepareForInterfaceBuilder
selector may also help you to implement IB specific code.More info about those 2 points here:
https://developer.apple.com/library/ios/recipes/xcode_help-IB_objects_media/chapters/CreatingaLiveViewofaCustomObject.html
Since
IBInspectable
values are set afterinitWithCoder:
and beforeawakeFromNib:
, you can set the defaults ininitWithCoder:
method.I wrote my code like this. It works pretty well for me, both when designing in the interface builder or running as a app.
Firstly I tried to override getter and did something like this:
But in that case I received issue about undeclared identifier
_borderColor
.Ok, I tried to avoid this issue via custom getter.
Actually it's not a proper getter, as we don't point this method as getter. In case we point we'll receive issue about undeclared identifier, that's why we won't.
Then we use this method for getting property value in updateUI method.
Also we have to override setter:
I'm using like that
if
propertyNameValue
hasnil
,propertyName
will returndefaultValue
.