Last updated on: May 27, 2023
Today, I’ll show you how to round only specific corners in a UIView using maskedCorners (iOS 11 and above) and UIBezierPath (iOS 10 and below).
Contents
iOS 11 and above
If your app is targeting only devices with iOS 11 and above, add the following code and replace myUIView with your UIView:
myUIView.clipsToBounds = true
myUIView.layer.cornerRadius = 5
myUIView.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
Code language: Swift (swift)
.layerMinXMinYCorner
= Top left corner.layerMaxXMinYCorner
= Top right corner.layerMinXMaxYCorner
= Bottom left corner.layerMaxXMaxYCorder
= Bottom right corner
iOS 10 and below
If your app supports iOS 10 and below, you can round specific corners using UIBezierPath:
if #available(iOS 11.0, *){
myUIView.clipsToBounds = true
myUIView.layer.cornerRadius = 5
myUIView.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
}else{
let path = UIBezierPath(roundedRect: myUIView.bounds,byRoundingCorners:[.topLeft, .topRight],cornerRadii: CGSize(width: 5, height: 5))
let maskLayer = CAShapeLayer()
maskLayer.path = path.cgPath
myUIView.layer.mask = maskLayer
}
Code language: Swift (swift)
If you have any questions, please feel free to leave a comment below