Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Round top corners of UIImageView like a UIButton

Here is my problem: I am trying to round the top corners of an UIImageView to look like a UIButton with rounded corner. I am using Masks to do that but the result I have isn't what I am really expecting...

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
  }
}

Calling it like this in my code:

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

And here is the result: Rounded corners error

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

Thanks you all for your help !

EDIT: I was using the right code, I just didn't notice that my UIImageView was larger than the UIImage hence the weird corners... I create the UIImageView programmatically and thus didn't notice the size difference... Newbie's mistake...

Thanks you all for you help !

like image 379
Dliix Avatar asked Jan 22 '16 11:01

Dliix


1 Answers

Use this value:

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

Result is

like image 95
Arun Ammannaya Avatar answered Oct 13 '22 10:10

Arun Ammannaya