you need first the clipsToBounds set to true and then if you know the image size, you can set its layer. cornerRadius to half of that size. Alternatively you can use the layoutSubviews method, and in its override access the imageView bounds. height and use half of this for the corner radius.
Select the view that you want to round and open its Identity Inspector. In the User Defined Runtime Attributes section, add the following two entries: Key Path: layer. cornerRadius , Type: Number, Value: (whatever radius you want)
You need to set the layer's masksToBounds
property to YES
:
cell.previewImage.layer.masksToBounds = YES;
This is because the UIImageView
control creates a pseudo-subview to hold the UIImage
object.
Also worth noting that
i.e if you have this
theImageView.contentMode = .scaleAspectFit
and
theImageView.layer.cornerRadius = (theImageView.frame.size.height)/2
theImageView.clipsToBounds = true
or
theImageView.layer.masksToBounds = true
It won't work. you will have to get rid of aspectFit code
//theImageView.contentMode = .scaleAspectFit
This should work
cell.previewImage.clipsToBounds = YES;
cell.previewImage.layer.cornerRadius = 20;
I believe you need to set:
cell.previewImage.layer.masksToBounds = YES;
cell.previewImage.layer.opaque = NO;
In Xcode Interface Builder, selecting 'Clip Subviews' Drawing attribute for the view together with setting the corner radius in the code cell.previewImage.layer.cornerRadius = 20;
does the job for me!
See 'Clip Subviews' option in IB
Try this below piece of code
cell.previewImage.layer.cornerRadius = 20;
cell.previewImage.clipsToBounds = YES;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With