0

I've followed other stackoverflow threads and created CAShapeLayer and added it to button layer.

let bazierPath = UIBezierPath.init(roundedRect: button.bounds, byRoundingCorners: [UIRectCorner.bottomLeft, UIRectCorner.topLeft], cornerRadii: CGSize(width: 10.0, height: 10.0 ))
            let shapeLayer = CAShapeLayer()
            shapeLayer.frame = button.bounds
            shapeLayer.path = bazierPath.cgPath
            shapeLayer.strokeColor = UIColor.green.cgColor
            shapeLayer.lineWidth = 1.0
            button.layer.mask = shapeLayer

but the problem is I'm getting corners with clear color but i want them to be green. check for buttons with "Mon" and "Fri" in following image for clear understanding about problem.

enter image description here

chandra mohan
  • 327
  • 1
  • 4
  • 19

2 Answers2

2

in your code you forgot to add the addSublayer to main layer contentView.layer.addSublayer(subLayer) for sample purpose

  let contentView = UIView(frame: CGRect(x: CGFloat(50), y: CGFloat(100), width: CGFloat(200), height: CGFloat(100)))
    self.view.addSubview(contentView)
    let subLayer = CAShapeLayer()
    subLayer.fillColor = UIColor.blue.cgColor
    subLayer.strokeColor = UIColor.green.cgColor
    subLayer.lineWidth = 1.0
    subLayer.path = UIBezierPath(roundedRect: contentView.bounds, byRoundingCorners: [UIRectCorner.bottomLeft, UIRectCorner.topLeft], cornerRadii: CGSize(width: 10.0, height: 10.0 )).cgPath
    contentView.layer.addSublayer(subLayer)

output

enter image description here

Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
1

Try to use the following function:

 fun createRoundCorners(corners:UIRectCorner, radius: CGFloat) 
  {
    let borderLayer = CAShapeLayer()
    borderLayer.frame = self.layer.bounds
    borderLayer.strokeColor = // Add your color
    borderLayer.fillColor = UIColor.clearColor().CGColor
    borderLayer.lineWidth = 1.0
    let path = UIBezierPath(roundedRect: self.bounds, 
      byRoundingCorners: corners, 
      cornerRadii: CGSize(width: radius, height: radius))
    borderLayer.path = path.CGPath
    self.layer.addSublayer(borderLayer);
  }
User511
  • 1,456
  • 10
  • 28