Button Border with Transparent Background in Swift
Setting the backgroundColor
to clearColor
makes the button transparent.
Try the code below for example. You can configure and vary the borderAlpha,cornerRadius and colours as your want.
let borderAlpha : CGFloat = 0.7
let cornerRadius : CGFloat = 5.0
button.frame = CGRectMake(100, 100, 200, 40)
button.setTitle("Get Started", forState: UIControlState.Normal)
button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
button.backgroundColor = UIColor.clearColor()
button.layer.borderWidth = 1.0
button.layer.borderColor = UIColor(white: 1.0, alpha: borderAlpha).CGColor
button.layer.cornerRadius = cornerRadius
Semi-transparent border on button
Use the button's layer's properties:
yourButton.layer.borderWidth = 3.0f;
yourButton.layer.borderColor = [UIColor colorWithRed:178/255.0 green:170/255.0 blue:156/255.0 alpha:0.4].CGColor;
(Change the values so it looks good, alpha makes it semi-transparent)
Also, you'll need this if you don't have the circle yet:
yourButton.layer.cornerRadius = yourButton.frame.size.width/2;
EDIT: As @holex suggested, a better way to calculate the cornerRadius is:
CGFloat radius = MIN(yourButton.frame.size.width, yourButton.frame.size.height) / 2.0
yourButton.layer.cornerRadius = radius;
Transparent background button with 2 sided corner radius in Swift
You can edit roundCorners
function like below;
extension UIButton {
func roundCorners(_ corners: UIRectCorner, radius: CGFloat, borderColor: UIColor, borderWidth: CGFloat) {
let path = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
let mask = CAShapeLayer()
mask.path = path.cgPath
layer.mask = mask
let borderLayer = CAShapeLayer()
borderLayer.path = path.cgPath
borderLayer.lineWidth = borderWidth
borderLayer.strokeColor = borderColor.cgColor
borderLayer.fillColor = UIColor.clear.cgColor
borderLayer.frame = bounds
layer.addSublayer(borderLayer)
}
}
and use it like:
let button = UIButton(frame: CGRect(x: 50, y: 50, width: 100, height: 40))
button.setTitle("Today", for: .normal)
button.roundCorners([.topLeft, .bottomLeft], radius: 25, borderColor: .white, borderWidth: 10)
Swift how change the background color and button border
you forgot to set UIButton
Border
button.layer.borderWidth = 0.8
UIButton Semi-Transparent Border
Set the color of a sublayer to the color you want the button to be (don't set the background color of the button itself), and inset its rect relative to the button's rect,
- (void) awakeFromNib {
self.layer.cornerRadius = 6.0f;
self.layer.borderWidth = 4.0f;
self.layer.borderColor = [[UIColor colorWithWhite:1.0f alpha:0.5f] CGColor];
CALayer *sub = [CALayer new];
sub.frame = CGRectInset(self.bounds, 4, 4);
sub.backgroundColor = [UIColor redColor].CGColor;
[self.layer addSublayer:sub];
}
Another way to do it, which will work better if you want the background color to be rounded too, is use a background color for both the self.layer and the sublayer. In this case theres on need to use a border at all.
- (void) awakeFromNib {
self.layer.cornerRadius = 6.0f;
self.tintColor = [UIColor whiteColor]; // make white text
self.layer.backgroundColor = [[UIColor colorWithWhite:1.0f alpha:0.4] CGColor];
CALayer *sub = [CALayer new];
sub.cornerRadius = 4.0f;
sub.frame = CGRectInset(self.bounds, 4, 4);
sub.backgroundColor = [UIColor blueColor].CGColor;
[self.layer addSublayer:sub];
}
SwiftUI - Button with transparent background
Thanks to Asperi to point me into the right direction.
I just had to add
.buttonStyle(PlainButtonStyle())
to the group.
SwiftUI: Button with image got gray border on real iPhone but works fine on simulation mode
in MainList
get rid of:
.background(.white)
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
Why does a selected image overflow the button border in swift?
Set buttons clip to bounds true
button.clipsToBounds = true
Related Topics
Perform a Segue Programmatically
How to Call a Static Function on a Protocol in a Generic Way
How to Perform a Curl Request in Swift
List All Available Audio Devices
Module File's Deployment Target Is iOS9.0 V9.0 with Xcode 7/Swift 2
Cannot Form Weak Reference to Instance of Class Nstextview
How to Check If Annotation Is Clustered (Mkmarkerannotationview and Cluster)
Interactivepopgesturerecognizer Corrupts Navigation Stack on Root View Controller
Swift 3: How to Pinch to Scale and Rotate Uiimageview
Create a Timer Publisher Using Swift Combine
Xcode Error: Missing Required Module 'Firebase'
Why Does Filters in Swift Iterate the Collection Twice
Xcode: Could Not Load Modelio.Framework, Scenekit.Framework, etc
How to Get Class Name of Parent Viewcontroller in Swift
How to Create a Hud on Top of My Scenekit.Scene
Guarantees About the Lifetime of a Reference in a Local Variable