Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rounded views not working in iOS 9

I have a code that takes a view and rounds it corners - turning it into a circle:

imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 8, 8)];
imageView.layer.cornerRadius = imageView.frame.size.width/2;
imageView.layer.masksToBounds = YES;

This works great on iOS 7 and iOS 8 - but doesn't work on iOS 9. How can I fix it?

like image 833
YogevSitton Avatar asked Aug 25 '15 08:08

YogevSitton


People also ask

How do you add corner radius to view?

You can give it round corners by changing the cornerRadius property of the view's layer . and smaller values give less rounded corners. Both clipsToBounds and masksToBounds are equivalent. It is just that the first is used with UIView and the second is used with CALayer .

What is corner radius in IOS?

Discussion. Setting the radius to a value greater than 0. 0 causes the layer to begin drawing rounded corners on its background. 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.


2 Answers

Turns out adding a background color to the ImageView fixes the issue...

like image 88
YogevSitton Avatar answered Oct 02 '22 20:10

YogevSitton


Thanks to @YogevSitton answer I've searched over docs and found this:

It appears to be something in apple 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.

So, to set cornerRadius to UIImageView it needs to have backgroundColor.

like image 32
eilas Avatar answered Oct 02 '22 19:10

eilas