Uibutton Borders Function Only Gives Back White Borders

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

iOS - How to Remove UIButton border color?

You can implement UIView extension this way,

extension UIView {
func roundCorners(_ corners: UIRectCorner, radius: CGFloat, borderColor: UIColor, borderWidth: CGFloat) {
let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))

let mask = CAShapeLayer()
mask.path = path.cgPath
self.layer.mask = mask

let borderPath = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
let borderLayer = CAShapeLayer()
borderLayer.path = borderPath.cgPath
borderLayer.lineWidth = borderWidth
borderLayer.strokeColor = borderColor.cgColor
borderLayer.fillColor = UIColor.clear.cgColor
borderLayer.frame = self.bounds
self.layer.addSublayer(borderLayer)
}
}

Use it like this,

button.backgroundColor = UIColor.brown
button.roundCorners([.allCorners], radius: 30.0, borderColor: UIColor.white, borderWidth: 10.0)

This will display output like this,

Sample Image

Above image contains button with white borderColor.

This will be generic solution for all buttons in your applications.

Let me know in case of any queries.

how to change bordercolor when Button.isHighlighted

The actions you are looking for are .touchDown and anything .touchUp:

override func viewDidLoad() {
super.viewDidLoad()
theButton.setTitle("Normal", for: .normal)
theButton.setTitle("Highlighted", for: .highlighted)
theButton.layer.borderColor = UIColor.red.cgColor
theButton.layer.borderWidth = 1
theButton.addTarget(self, action: #selector(startHighlight), for: .touchDown)
theButton.addTarget(self, action: #selector(stopHighlight), for: .touchUpInside)
theButton.addTarget(self, action: #selector(stopHighlight), for: .touchUpOutside)
}
func startHighlight(sender: UIButton) {
theButton.layer.borderColor = UIColor.green.cgColor
theButton.layer.borderWidth = 1
}
func stopHighlight(sender: UIButton) {
theButton.layer.borderColor = UIColor.red.cgColor
theButton.layer.borderWidth = 1
}

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

Extended UIButton border is not initially drawn

This works

public class MyButton : UIButton
{
public MyButton() : base(UIButtonType.RoundedRect) { }

public override RectangleF Frame {
get {
return base.Frame;
}
set {
var temp = TranslatesAutoresizingMaskIntoConstraints;
TranslatesAutoresizingMaskIntoConstraints = false;
var constraints = new [] {
NSLayoutConstraint.Create(this, NSLayoutAttribute.Width, NSLayoutRelation.Equal, null, NSLayoutAttribute.NoAttribute, 1.0f, value.Width),
NSLayoutConstraint.Create(this, NSLayoutAttribute.Height, NSLayoutRelation.Equal, null, NSLayoutAttribute.NoAttribute, 1.0f, value.Height)
};
AddConstraints(constraints);
SizeToFit();
RemoveConstraints(constraints);
base.Frame = value;
TranslatesAutoresizingMaskIntoConstraints = temp;
}
}
}

This is only a workaround, it appears to be a bug. The SizeToFit() fixes the issue, the other code maintains the frame.

Change UIButton border color after tapping it

Try this one.

- (instancetype)init
{
self = [super init];
if (self) {
[self addCustomFeature];
}
return self;
}

- (void)awakeFromNib
{
[super awakeFromNib];
[self addCustomFeature];
}

#pragma mark CustomizeUI
- (void)addCustomFeature
{
// creating curved corners
[self.layer setCornerRadius:5];
[self.layer setMasksToBounds:YES];

}

- (void)setBorderColor:(UIColor *)borderColor
{
[self.layer setBorderWidth:1.0f];
[self.layer setBorderColor:borderColor.CGColor];
}

- (void) setHighlighted:(BOOL)highlighted {
[super setHighlighted:highlighted];
if (highlighted) {
self.borderColor = [UIColor redColor];
}
else {
self.borderColor = [UIColor greenColor];
}
}

Why does my custom UIButton appear distorted with two borders in my Xcode project?

The plus_photo image has a circle.

    button.setImage(UIImage(named: "plus_photo"), for: .normal)

Then you are adding another a circle when you make a round border and set its border color to white.

    button.layer.borderColor = UIColor.white.cgColor

Double border to a UIButton

If you just want a simple outline (it's a little hard to tell from your image what the button is), create the button with UIButtonTypeCustom (so you get a blank slate) then set its background color to be black. next, get the CALayer of the button (through the layer method) and set the borderColor and borderWidth properties as desired.

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

Sample Image

Here is an image you can use to test.
Sample Image

Swift: UIButton Customization

Take a look at Swift's Extensions. You could pretty easily do something like

extension UIButton {
func setPasswordBorderColor(borderColor: UIColor) {
//White Border
let passwordBorder = CALayer()

let width = CGFloat(5.0)

passwordBorder.borderColor = borderColor.CGColor

passwordBorder.frame = CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height)

passwordBorder.borderWidth = width

self.layer.addSublayer(passwordBorder)

self.layer.masksToBounds = true
}
}


Related Topics



Leave a reply



Submit