Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Corner Radius on UIImageView not working

People also ask

How do I round UIImageView?

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.

How do you change the corner radius of a storyboard?

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

  1. If you are using aspectFit AND cornerRadius with clipsToBounds/masksToBounds, you won't get the rounded corners.

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
  1. Make sure the width and the height for the Image View is same

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;