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
Set border for UIButton
layer.borderColor must be CGColor, so code below will work.
_myButton.layer.borderWidth=2.0f;
_myButton.layer.borderColor=[ UIColor lightGrayColor ].CGColor;
Your code
[UIColor lightGrayColor]
returns a UIColor instance, not a CGColor instance. And UIColor can't bridge to CGColor, so your cast
(__bridge CGColorRef _Nullable)
returns unexpected result.
You can see strange result using this code
NSLog( @"%@", (__bridge CGColorRef _Nullable)([UIColor lightGrayColor]) );
returning below. ( Xcode 7.3 )
2016-04-06 15:30:51.415 36442877[8570:2643221] UIDeviceWhiteColorSpace 0.666667 1
If you give borderColor CGColor instance directory, you don't need casting.
Set a border to UIButton with Star Image
You may try to create a system button, and change the tint color of the button based on your requirement
Make sure the star image just consist of the star and rest of the part is transparent.
Then try changing the tintColor of the button
Here is an image you can use to test.
How can I make a button have a rounded border in Swift?
Use button.layer.cornerRadius
, button.layer.borderColor
and button.layer.borderWidth
.
Note that borderColor
requires a CGColor
, so you could say (Swift 3/4):
button.backgroundColor = .clear
button.layer.cornerRadius = 5
button.layer.borderWidth = 1
button.layer.borderColor = UIColor.black.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 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).
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.
Related Topics
Uicollectionview with Paging - Setting Page Width
Two iOS Apps Using the Same Facebook App Id - Is It Possible
Uitableview Reloaddata Automatically Calls Resignfirstresponder
How Does the Whatsapp Web Client Still Work with the Latest iOS Update (Sdk Version 13.0+)
How to Do Authentication in Uiwebview Properly
Hide Device Volume Hud View While Adjusitng Volume with Mpvolumeview Slider
Uiview's Border Color in Interface Builder Doesn't Work
Custom Url to Launch Facebook Messenger on iOS
How to Configure Full Urls in Xcconfig Files
How to Draw a Circle in iOS Swift
How to Properly Order Data from Firebase Chronologically
Uitableviewcell Set Selected Initially
iOS Storyboard Passing Data Navigationviewcontroller