1

I want to apply different corner radius value to different corners of UIView in iOS using swift. It means topLeft, topRight, bottomLeft, bottomRight corners of UIView should have different corner radiuous(i.e 1, 5, 10, 15). How can be implement it?

I tried applying cornerRadius in same way but It overrides the previous cornerRadius values.

Roshan
  • 712
  • 4
  • 17
  • https://stackoverflow.com/questions/10167266/how-to-set-cornerradius-for-only-top-left-and-top-right-corner-of-a-uiview – Shamas S May 14 '18 at 08:22
  • Possible duplicate of [how to set cornerRadius for only top-left and top-right corner of a UIView?](https://stackoverflow.com/questions/10167266/how-to-set-cornerradius-for-only-top-left-and-top-right-corner-of-a-uiview) – Ahmad F May 14 '18 at 08:23
  • 1
    You can also go through [this tutorial](https://www.appcoda.com/rounded-corners-uiview/) – Imad Ali May 14 '18 at 08:23
  • check this answer https://stackoverflow.com/questions/45237863/how-to-control-an-uiviews-rounded-corners-separately-using-ibinspectable-in-sw/45245114#45245114 – Reinier Melian May 14 '18 at 08:24
  • @ShamasS I tried that but when I apply cornerRadius to topLeft and topRight first with radius 5. then apply radius 10 to bottomLeft and bottomRight. in that case apply changes got overridden. Can I not provide different radious values to different corners? – Ganesh Kakade May 14 '18 at 08:25
  • 1
    @ReinierMelian Thanks. This what I was looking for. At least, I got gist from it. – Ganesh Kakade May 14 '18 at 08:34

1 Answers1

0

If your deployment target is abouve iOS 10 then you should use maskedCorners

if #available(iOS 11.0, *) {
    customeView.clipsToBounds = true
    customeView.layer.cornerRadius = 5
    customeView.layer.maskedCorners = [.layerMaxXMaxYCorner, .layerMinXMaxYCorner, .layerMaxXMinYCorner, .layerMinXMinYCorner]
} 

else try this

let path = UIBezierPath(roundedRect: customeView.bounds, byRoundingCorners:[.bottomLeft, .bottomRight], cornerRadii: CGSize(width: 5, height: 5))
            let mask = CAShapeLayer()
            mask.path = path.cgPath
            customeView.layer.mask = mask
  • 1
    This doesn't work, since it only allows the corners to be rounded to one value or not rounded at all. The original poster wanted different corners rounded to different degrees. – SuddenMoustache Jan 05 '21 at 11:41