Taylor Taylor - 2 months ago 33
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


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