Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I do programmatically gradient border color UIButton with Swift [closed]

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

Gradient button]

Thanks for help

like image 590
m.akyol Avatar asked Apr 25 '16 09:04

m.akyol


People also ask

How do you make a gradient background in Swift?

There are many ways to create background gradients, below is just one simple approach: In a new Xcode iOS project, open Main. storyboard, from the Object Library drag a new View onto the View Controller. Set the View's top, bottom, left and right constraints to be zero and ensure 'Constrain to margins' is deselected.


1 Answers

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)

Swift 3 version:

    let gradient = CAGradientLayer()
    gradient.frame =  CGRect(origin: CGPoint.zero, size: self.myButton.frame.size)
    gradient.colors = [UIColor.blue.cgColor, UIColor.green.cgColor]

    let shape = CAShapeLayer()
    shape.lineWidth = 2
    shape.path = UIBezierPath(rect: self.myButton.bounds).cgPath
    shape.strokeColor = UIColor.black.cgColor
    shape.fillColor = UIColor.clear.cgColor
    gradient.mask = shape

    self.myButton.layer.addSublayer(gradient)
like image 144
gvuksic Avatar answered Oct 17 '22 16:10

gvuksic