I can't get a border onto my buttons in Xcode 5 without setting them directly in the code. Is it possible that there's no way to do this on the storyboard without making a custom background image?
5 Answers
You can use key path.
For example the corner radius (layer.cornerRadius
) as describe on the image.
You will not be able to see the effects on storyboard, cause this parameters are evaluated at runtime. Now you can use a swift category in UIView (code bellow the picture) in with @IBInspectable
to show the result at the storyboard (If you are using the category, use only cornerRadius
and not layer.cornerRadius
as a key path.
extension UIView {
@IBInspectable var cornerRadius: CGFloat {
get {
return layer.cornerRadius
}
set {
layer.cornerRadius = newValue
layer.masksToBounds = newValue > 0
}
}
}
Here is category from Peter DeWeese answer that allow use keypath layer.borderUIColor
to set the border color.
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

- 1
- 1

- 15,135
- 7
- 59
- 96
-
3You learn something new every day.. this would have been useful to know many months ago hah. – RyanG Dec 09 '13 at 18:43
-
2Wow, this is so cool! I didn't know about this hidden gem! Upvoted. – TotoroTotoro Dec 09 '13 at 18:45
-
3BTW, I also had to set `layer.borderWidth` for the border to show. – TotoroTotoro Dec 09 '13 at 18:48
-
That is wild! Thanks so much Guilherme, and I have a feeling that's going to come in handy in the future – Tommy Nicholas Dec 09 '13 at 18:54
-
Can I set the layer.borderColor? That doesn't seem to be working. – Tommy Nicholas Dec 09 '13 at 19:03
-
1It's because layer.boderColor expected a CGColor, not an UIColor. But you can make a category as workaround. wait a minute, i will update my answer – Guilherme Torres Castro Dec 09 '13 at 20:29
-
Magic! Wow, so the nomenclature of CALayer+XibConfiguration makes it configure layers in XibConfigurations? It worked for me but I don't understand how! Anyway, thank you so much Guilherme. Great answer. – Tommy Nicholas Dec 11 '13 at 16:47
-
My LAST question, if you have a second, is would any of this allow for just adding a top border, or just a left border, etc. If not I see some workarounds I can try, but just curious - it's not critical to what I'm doing, I just feel like this is going to be a popular q/a so if it could be more complete and you already know how to do it, why not add it? – Tommy Nicholas Dec 11 '13 at 17:32
-
Tommy, I will update my answer with a workaround for this, but i don't have time to test right now – Guilherme Torres Castro Dec 11 '13 at 17:48
-
@GuilhermeTorresCastro I'm a little new to iOS development. Are border color snippets in a separate file? How do you use it once implemented? – Noah Passalacqua Mar 12 '14 at 01:31
-
Yes @NoahPassalacqua, It's a category (search for it in google, to get some explanation on this). Once you have added the category to your project you can use: layer.borderUIColor keypath at interface builder. – Guilherme Torres Castro Mar 12 '14 at 03:39
-
There is only a slight problem with this approach. When a modal view is presented, the border color of the button doesn't change. It looks weird with other standard controls like a segmented control which gray out when a modal view is presented. Because of this, I'm using a subclass of UIButton. However, when a view is presented, I'm unable to detect it and change the border color to gray. Bummer... – nemesys Apr 22 '15 at 16:50
-
I just tried it and am not seeing the changes in the rounder corners show in my storyboard. However, I do see the changes when running the file. I'm on 6.3.2. Any suggestions? – Matthew Chung Jun 10 '15 at 14:56
-
@MatthewChung to see the changes in IB you have to make a category the UIView , and not for the CALayer. Which one did you used ? – Guilherme Torres Castro Jun 10 '15 at 15:31
-
Thanks for writing @GuilhermeTorresCastro . I am using the swift extension. I know this is probably asking too much, but i made a trivial project with 1 view and the extension and it is still not displaying rounded corners in the storyboard. If you have 2 minutes, it would be very very much appreciated. https://github.com/captainchung/test – Matthew Chung Jun 10 '15 at 16:14
-
It would be worthwhile also showing a screen shot of the addition UI element that appears once you've a swift extension in place, so it does not have to be done in the user defined values area. – Faisal Memon Oct 18 '15 at 10:15
Swift 3 If you want to see the result in IB when you use IBInspectable, you have to extend UIView and add the properties to that class, i.e.
@IBDesignable class MyView: UIView {}
extension MyView {
@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
layer.masksToBounds = newValue > 0
}
}
@IBInspectable var borderColor: UIColor {
get {
return UIColor.init(cgColor: layer.borderColor!)
}
set {
layer.borderColor = newValue.cgColor
}
}
}

- 9,048
- 3
- 25
- 20
Short answer :
layer.cornerRadius = 10
layer.borderWidth = 1
layer.borderColor = UIColor.blue.cgColor
Long answer :
Rounded Corners UIButton
customUIView.layer.cornerRadius = 10
Border Thickness
pcustomUIView.layer.borderWidth = 1
Border Color
customUIView.layer.borderColor = UIColor.blue.cgColor

- 35,607
- 26
- 136
- 135
you can set your UIButton User Defined Run Time Attributes ie borderWidth, cornerRadius, borderColor etc. As shown in the image. Note:- don't use layer. before the attribute name, it will not work.

- 587
- 5
- 9
-
2Tried, it doesn't work, can you provide a link to documentation why this should work? or an example. I guess you mean you need first write extension with IBInspectable vars like in other answers. Answer need to be full – Zaporozhchenko Oleksandr Apr 07 '21 at 15:53
You can use a piece of code like:
self.addButton.layer.borderColor = [[UIColor greenColor] CGColor];
Please note: addButton
is an IBOutlet.

- 374
- 1
- 7
- 22

- 1,522
- 2
- 18
- 34