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
Update:
select appropriate Type for Keypath:
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:
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":
If you don't want to use a configuration, and instead want the pre-iOS 15 behaviour, change "Style" to "Default":
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
Uitableviewcelldeleteconfirmationcontrol Issue
Change Width of a Uibarbuttonitem in a Uinavigationbar
How to Express Strings in Swift Using Unicode Hexadecimal Values (Utf-16)
Alert View Is Showing White Rectangle in iOS7
Mkmapview Show Incorrectly Saved Region
Are View Controllers with Nib Files Broken in iOS 8 Beta 5
Sending a Private Message to Your Friends via Facebook iOS Sdk
How to Call Method from Viewcontroller in Gamescene
Find Attributes from Attributed String That User Typed
How to Tap on a Specific Point Using Xcode Uitests
iOS 6.X Open Command Line on Jailbreak
Can't Cast Value of Type Uiviewcontroller to Patterndetailviewcontroller
iOS - Push Viewcontroller from Code and Storyboard
How to Calculate Actual Font Point Size in iOS 7 (Not the Bounding Rectangle)
Setting "Applelanguages" Doesn't Change App Language
iOS Getting Location Updates When App Terminated Without Using Significantchange
Xcode 6 Upload App with Error: Itunes Store Operation Failed Error Description Not Available