I am trying to set up a view's layer properties via IB. Everything works except for color of the border (property layer.borderColor
):
I remember running into this problem a year ago and I ended up doing it programatically. And still, I can do this programmatically, but I am curious why the layer.borderColor
property never works via interface builder. I don't want to import QuartzCore
, and then write extra line of code just because of this, seems like an overkill.
You can set a value for the "borderColor" key in the XIB and use:
Here's a quick way to overcome this. Categories...
You don't have to store it, it's just nice so you can query later. The important thing is taking the value and assigning the UIColor's CGColor to the layer.
Of course, in the Interface Builder you're not setting the value on
layer.borderColor
, rather just onborderColor
.You can customise border with 2 methods. First one is this. Just click on the object go to the identity inspector and set the attributes.
Second one is this. make an IBOutlet of required object and put this code in view did load.
Use IBDesignable instead of Runtime Attributes it is more clear.
Put this code in any class and edit the properties direct on the storyboard.
You have to create Category for CALayer:
CALayer+UIColor.h
CALayer+UIColor.m
And then in User Defined Runtime attributes You can use it as it is on image below:
For Swift it is much more simple:
Then in Xcode you can use it like this:
Once you choose sth it is automatically added to your runtime attributes:
It's possible to do this, but it's not a built-in feature. This is because the
Color
type in the User Defined Runtime Attributes panel creates aUIColor
, butlayer.borderColor
holds aCGColorRef
type. Unfortunately, there's no way to assign aCGColorRef
type in Interface Builder.However, this is possible through a proxy property. See Peter DeWeese's answer to a different question for a possible solution to this problem. His answer defines a category that allows a proxy color to be set through Interface Builder.