Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Button Border with Transparent Background in Swift

Tags:

swift

uibutton

How can I make a UIButton border to look alike in the below image (the "Getting Started") button with a transparent background?

How should I achieve this using storyboard or how to do it programmatically?

enter image description here

like image 780
loop Avatar asked Feb 13 '15 04:02

loop


1 Answers

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
like image 112
rakeshbs Avatar answered Sep 28 '22 07:09

rakeshbs