How to set corner radius of imageView?

theDC picture theDC · Jan 9, 2015 · Viewed 112.5k times · Source

In Objective-C such line

self.mainImageView.layer.cornerRadius = CGRectGetWidth(self.mainImageView.frame)/4.0f;

does its job, I tried it in Swift using analogy

self.mainImageView.layer.cornerRadius = CGRectGetWidth(self.mainImageView.frame)/4.0

and it doesn't change anything, the corners are the same as before. Moreover, Xcode does not show any syntax errors. Does Swift support any other way to reach this goal? I checked some other threads here and usually it's getting done in Swift in the way showed above.

Answer

DarthMike picture DarthMike · Jan 9, 2015

Layer draws out of clip region, you need to set it to mask to bounds:

self.mainImageView.layer.masksToBounds = true

From the docs:

By default, the corner radius does not apply to the image in the layer’s contents property; it applies only to the background color and border of the layer. However, setting the masksToBounds property to true causes the content to be clipped to the rounded corners