Search code examples
iosswiftuibuttoncashapelayercgcolor

Changing UIButton border colours using layer


I have a UIButton, I have put a border on the top and bottom of the UILabel. While changing the colour of the line, below I have set the line to white. I can change the line to a predetermined colour such as green using UIColor.green.cgColor. The problem is I want to set the colour to topBorder.strokeColor = UIColor.init(red: 50, green: 50, blue: 50, alpha: 0.5).cgColor.

When I do this the colour still comes out white. Why does this happen? I can set it to a predetermined colour but it can set it to the specific colour that I want.

let topBorderTerms = CAShapeLayer()
let topPathTerms = UIBezierPath()
topPathTerms.move(to: CGPoint(x: 0, y: 0))
topPathTerms.addLine(to: CGPoint(x: Terms.frame.width, y: 0))
topBorderTerms.path = topPath.cgPath
topBorderTerms.strokeColor = UIColor.white.cgColor
topBorderTerms.lineWidth = 1.0
topBorderTerms.fillColor = UIColor.white.cgColor
Terms.layer.addSublayer(topBorderTerms)

enter image description here


Solution

  • Replace

    topBorder.strokeColor = UIColor.init(red: 50, green: 50, blue: 50, alpha: 0.5).cgColor. 
    

    with

    topBorder.strokeColor = UIColor(red: 50/255, green: 50/255, blue: 50/255, alpha: 0.5).cgColor
    

    You miss divide by 255