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.cornerRadiuslayer.borderWidthlayer.borderColor
Update: layer.masksToBounds = true
Rich86Man's answer is correct, but you can use categories to proxy properties such as layer.borderColor. (From the ConventionalC CocoaPod)
CALayer+XibConfiguration.h:
#import <QuartzCore/QuartzCore.h>#import <UIKit/UIKit.h>@interface CALayer(XibConfiguration)// This assigns a CGColor to borderColor.@property(nonatomic, assign) UIColor* borderUIColor;@end
CALayer+XibConfiguration.m:
#import "CALayer+XibConfiguration.h"@implementation CALayer(XibConfiguration)-(void)setBorderUIColor:(UIColor*)color{ self.borderColor = color.CGColor;}-(UIColor*)borderUIColor{ return [UIColor colorWithCGColor:self.borderColor];}@end
layer.borderUIColor
The result will be apparent during runtime, not in Xcode.
Edit: You also need to set layer.borderWidth
to at least 1 to see the border with the chosen color.
In Swift 2.0:
extension CALayer { var borderUIColor: UIColor { set { self.borderColor = newValue.CGColor } get { return UIColor(CGColor: self.borderColor!) } }}
In Swift 3.0:
extension CALayer { var borderUIColor: UIColor { set { self.borderColor = newValue.cgColor } get { return UIColor(cgColor: self.borderColor!) } }}
Similar answer to iHulk's one, but in Swift
Add a file named UIView.swift in your project (or just paste this in any file) :
import UIKit@IBDesignable extension UIView { @IBInspectable var borderColor: UIColor? { set { layer.borderColor = newValue?.cgColor } get { guard let color = layer.borderColor else { return nil } return UIColor(cgColor: color) } } @IBInspectable var borderWidth: CGFloat { set { layer.borderWidth = newValue } get { return layer.borderWidth } } @IBInspectable var cornerRadius: CGFloat { set { layer.cornerRadius = newValue clipsToBounds = newValue > 0 } get { return layer.cornerRadius } }}
Then this will be available in Interface Builder for every button, imageView, label, etc. in the Utilities Panel > Attributes Inspector :
Note: the border will only appear at runtime.