func roundCorners(corners:UIRectCorner, radius: CGFloat) {
let path = UIBezierPath(roundedRect: self.bounds,
byRoundingCorners: corners,
cornerRadii: CGSize(width: radius, height: radius))
let maskLayer = CAShapeLayer( )
maskLayer.frame = self.bounds
maskLayer.path = path.cgPath
self.layer.mask = maskLayer
}
Use myView.roundCorners (corner:[.topLeft,.topRight],radius:radius)
But it’s around the side of the view:
This is in the tableView sectionHeader, if I scroll down and then use the same code to round up:
The top discount view also uses the same function for rounding.
Thanks for the help.
Update
If I fix the width on the view, then it works fine.
Swift 4.2:
extension UIView {
func roundCorners(_ corners: UIRectCorner, radius: CGFloat ) {
if #available(iOS 11.0, *) {
clipsToBounds = true
layer.cornerRadius = radius
layer.maskedCorners = CACornerMask(rawValue: corners.rawValue)
} else {
let path = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
let mask = CAShapeLayer()
mask .path = path.cgPath
layer.mask = mask
}
}
}
I am trying to use the following
func roundCorners(corners:UIRectCorner, radius: CGFloat) {
let path = UIBezierPath(roundedRect: self.bounds,
byRoundingCorners: corners,
cornerRadii: CGSize(width: radius, height: radius))
let maskLayer = CAShapeLayer()
maskLayer.frame = self.bounds
maskLayer.path = path.cgPath
self.layer.mask = maskLayer
}
Use myView.roundCorners(corner:[.topLeft,.topRight],radius:radius)
< p>But it is revolving around the viewpoint:
This is in the tableView sectionHeader, if I scroll down and then round up using the same code:
The top discount view also uses the same function for rounding.
Thanks for the help.
Update
If I fix the view on Width, then it works fine.
iOS 11 introduces maskedCorners, which can get smoother and better quality results. You can still call the function Use UIRectCorner and convert it to CACornerMask:
Swift 4.2:
extension UIView {
func roundCorners( _ corners: UIRectCorner, radius: CGFloat) {
i f #available(iOS 11.0, *) {
clipsToBounds = true
layer.cornerRadius = radius
layer.maskedCorners = CACornerMask(rawValue: corners.rawValue)
} else {< br /> let path = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
let mask = CAShapeLayer()
mask.path = path.cgPath
layer.mask = mask
}
}
}