Skip to content

Instantly share code, notes, and snippets.

@shaildyp
Last active December 7, 2022 12:14
Show Gist options
  • Save shaildyp/6cd1de39498ff7b3c22fa758f005f7cd to your computer and use it in GitHub Desktop.
Save shaildyp/6cd1de39498ff7b3c22fa758f005f7cd to your computer and use it in GitHub Desktop.
Backward compatible maskedCorners of iOS 11.
extension UIView {
func roundCorners(_ corners: CACornerMask, radius: CGFloat) {
if #available(iOS 11, *) {
self.layer.cornerRadius = radius
self.layer.maskedCorners = corners
} else {
var cornerMask = UIRectCorner()
if(corners.contains(.layerMinXMinYCorner)){
cornerMask.insert(.topLeft)
}
if(corners.contains(.layerMaxXMinYCorner)){
cornerMask.insert(.topRight)
}
if(corners.contains(.layerMinXMaxYCorner)){
cornerMask.insert(.bottomLeft)
}
if(corners.contains(.layerMaxXMaxYCorner)){
cornerMask.insert(.bottomRight)
}
let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: cornerMask, cornerRadii: CGSize(width: radius, height: radius))
let mask = CAShapeLayer()
mask.path = path.cgPath
self.layer.mask = mask
}
}
}
@shailesh-bbc
Copy link

I don't even remember this code anymore. Glad that it is still useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment