Uiview's Border Color in Interface Builder Doesn't Work

UIView's border color in Interface builder doesn't work?

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 a UIColor, but layer.borderColor holds a CGColorRef type. Unfortunately, there's no way to assign a CGColorRef 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.

Is it possible to set UIView border properties from interface builder?

Actually you can set some properties of a view's layer through interface builder. I know that I can set a layer's borderWidth and cornerRadius through xcode. borderColor doesn't work, probably because the layer wants a CGColor instead of a UIColor.

You might have to use Strings instead of numbers, but it works!

layer.cornerRadius
layer.borderWidth
layer.borderColor

Update:
layer.masksToBounds = true

example

Update:
select appropriate Type for Keypath:

Sample Image

View border Color not changing

You are facing this issue because layer.borderColor want CGColor and from User defined runtime attributes you can only set UIColor not CGColor, when you don't set the color it will take default borderColor and i.e black color. To set borderColor you need to set it programmatically like this.

Swift 3

yourView.layer.borderColor = UIColor.red.cgColor //set your color here

Swift 2.3 or lower

yourView.layer.borderColor = UIColor.redColor().CGColor //set your color here

Change UIButton BorderColor in Storyboard

For Swift:

Sample Image

Swift 3:

extension UIView {

@IBInspectable var cornerRadius: CGFloat {
get {
return layer.cornerRadius
}
set {
layer.cornerRadius = newValue
layer.masksToBounds = newValue > 0
}
}

@IBInspectable var borderWidth: CGFloat {
get {
return layer.borderWidth
}
set {
layer.borderWidth = newValue
}
}

@IBInspectable var borderColor: UIColor? {
get {
return UIColor(cgColor: layer.borderColor!)
}
set {
layer.borderColor = newValue?.cgColor
}
}
}

Swift 2.2:

extension UIView {

@IBInspectable var cornerRadius: CGFloat {
get {
return layer.cornerRadius
}
set {
layer.cornerRadius = newValue
layer.masksToBounds = newValue > 0
}
}

@IBInspectable var borderWidth: CGFloat {
get {
return layer.borderWidth
}
set {
layer.borderWidth = newValue
}
}

@IBInspectable var borderColor: UIColor? {
get {
return UIColor(CGColor: layer.borderColor!)
}
set {
layer.borderColor = newValue?.CGColor
}
}
}

Setting insets with Interface Builder doesn't work

In iOS 15, the UIButton.Configuration API was added, and this added a few more options to IB, as well as changes to how buttons look. The tutorial was likely written before this, and did not take this into account.

Currently, every button you add in IB will by default use a UIButton.Configuration. This is what gives it the insets, which is part of the default configuration. In addition to setting this in code, it can also be set in IB under "Background Configuration":

Sample Image

If you don't want to use a configuration, and instead want the pre-iOS 15 behaviour, change "Style" to "Default":

Sample Image

Runtime attributes border color

Actually, you are using wrong attribute.The, correct attribute is layer.borderColor.
But again it will not work because it is type of CGColor and from IB we can only assign UIColor, we can't assign CGColor.

Eihter you can simply do it programaticlly.

Or

You can create extension with type CGColor.



Related Topics



Leave a reply



Submit