m.akyol m.akyol - 1 month ago 21
iOS Question

How can I do programmatically gradient border color UIButton with Swift

How can I design programmatically UIButton like this gradient border color?

http://i.stack.imgur.com/9j8yU.jpg

Thanks for help

Answer
let gradient = CAGradientLayer()
gradient.frame =  CGRect(origin: CGPointZero, size: self.myButton.frame.size)
gradient.colors = [UIColor.blueColor().CGColor, UIColor.greenColor().CGColor]

let shape = CAShapeLayer()
shape.lineWidth = 2
shape.path = UIBezierPath(rect: self.myButton.bounds).CGPath
shape.strokeColor = UIColor.blackColor().CGColor
shape.fillColor = UIColor.clearColor().CGColor
gradient.mask = shape

self.myButton.layer.addSublayer(gradient)