Taylor Taylor - 1 year ago 154
Swift Question

Button Border with Transparent Background in Swift

How can I make a

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

Answer Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download