Swift set UIButton setBorderColor in Storyboard
Here's my UIButton subclass:
@IBDesignable
public class Button: UIButton {
@IBInspectable public var borderColor:UIColor? {
didSet {
layer.borderColor = borderColor?.cgColor
}
}
@IBInspectable public var borderWidth:CGFloat = 0 {
didSet {
layer.borderWidth = borderWidth
}
}
@IBInspectable public var cornerRadius:CGFloat {
get {
return layer.cornerRadius
}
set {
layer.cornerRadius = newValue
layer.masksToBounds = newValue > 0
}
}
}
EDIT:
I created a simple project and added the above IBDesignable subclass. Below are three screen shots of the result. Note that in the Identity inspector I've set the Class to be Button, not UIButton and that it reports that the Designables are "Up to date". Note that in the Attributes inspector these appear as properties at the very top.
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
}
}
}
Set a border for UIButton in Storyboard
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
How to create border in UIButton?
You can set the border properties on the CALayer by accessing the layer property of the button.
First, add Quartz
#import <QuartzCore/QuartzCore.h>
Set properties:
myButton.layer.borderWidth = 2.0f;
myButton.layer.borderColor = [UIColor greenColor].CGColor;
See:
https://developer.apple.com/documentation/quartzcore/calayer#//apple_ref/occ/cl/CALayer
The CALayer in the link above allows you to set other properties like corner radius, maskToBounds etc...
Also, a good article on button fun:
https://web.archive.org/web/20161221132308/http://www.apptite.be/tutorial_custom_uibuttons.php
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:
How to add a button with border to storyboard in Xcode 6
You can add Runtime Attribute in storyboard
select your button
How to set the custom border color of UIView programmatically?
If you Use Swift 2.0+
self.yourView.layer.borderWidth = 1
self.yourView.layer.borderColor = UIColor(red:222/255, green:225/255, blue:227/255, alpha: 1).cgColor
How to add only a TOP border on a UIButton?
UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, btn.frame.size.width, 1)];
lineView.backgroundColor = [UIColor redColor];
[btn addSubview:lineView];
you can do the same for each border. Adding multiple UIViews you can add bottom and left or top and right or any border you want.
i.e. bottom & left:
UIView *bottomBorder = [[UIView alloc] initWithFrame:CGRectMake(0, btn.frame.size.height - 1.0f, btn.frame.size.width, 1)];
bottomBorder.backgroundColor = [UIColor redColor];
UIView *leftBorder = [[UIView alloc] initWithFrame:CGRectMake(1, 0, 1, btn.frame.size.height)];
leftBorder.backgroundColor = [UIColor redColor];
[btn addSubview:bottomBorder];
[btn addSubview:leftBorder];
if you don't use ARC, remember to release UIViews after adding subviews (or use autorelease).
Related Topics
Comma Automatically Being Added to Textfield in Swift
Cannot Convert Value of Type 'Nsmutablearray' to Expected Argument Type '[Skaction]'
Add [Unowned Self] to The Closure Argument Swift
Macos App Sandboxing - Read Access to Referenced Files from Parsed Xml
Swift Unsafemutablepointer & Unsafemutablepointer<Unsafepointer<Sometype>>
How to Force Sktextureatlas Created from a Dictionary to Not Modify Textures Size
Cast While Looping Over Dictionary in Swift
Automatically Reload Tableviewcontroller on Rewind
Sdwebimage, Swift: Sdwebimagerefreshcached Unresolved Identifier
Parse Weird Bug in Swift That Causes Acl Write Permissions to Change to an Objectid
Trouble Calling a Method in an Init
How to Do Imageview.Startanimating() with Completion in Swift
How to Create Rounded Image with Border and Shadow as Mkannotationview in Swift
Core Data with Swiftui Mvvm Feedback
How to Check Dictionary Value Is Empty or Not
Filter, Closure, Functional Syntax Version of for Loop with Multiple Conditions
Swift: Get The Compile Time Name of Variable (Referencing to a Class)
How to Display an Alert Controller When Nsdate == to a Time (For Example 12:00 Am) in Swift