UIImageView round top corners such as UIButton

Here is my problem: I am trying to combine the top corners of a UIImageView to look like a UIButton with a rounded corner. I use Masks for this, but the result that I have is not what I really expect ...

I am using the following extension:

extension UIView { func roundCorners(corners:UIRectCorner, radius: CGFloat) { let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius)) let mask = CAShapeLayer() mask.path = path.CGPath self.layer.mask = mask } } 

Call this in my code:

 imageView.roundCorners([.TopLeft, .TopRight], radius: 80) 

And here is the result: Rounded corner error

I would like the top corner to look like the bottom corners (bottom corners are UIButton corners of radius 10), but I can't see where the error is ...

Thank you all for your help!

EDIT: I used the correct code, I just did not notice that my UIImageView was larger than UIImage, therefore, strange angles ... I create a UIImageView programmatically and therefore did not pay attention to the difference in size ... Newbie error. ..

Thank you all for your help!

+5
source share
1 answer

Use this value:

 imageView([.TopLeft, .TopRight], radius: 20) 

Result

+1
source

Source: https://habr.com/ru/post/1241223/


All Articles