Reputation: 63
I've searched around and still can't seem to get this right. I'm trying to achieve a circular image in iOS 8. Before Xcode 6, I think the answer would've been imageView.layer.cornerRadius
property. I've tried this but in Xcode 6 and iOS 8 now using Auto Layout, which I think is causing the issue, it doesn't seem to work correctly. The shape doesn't come out as a circle but as some eye looking shape or a deformed oval, its never a perfect circle. Is there any other way of achieving this still with Autolayout?
Upvotes: 5
Views: 7407
Reputation: 2020
Try this code, corner radius should be half of the width or height of the provided photo view and it must be square in shape.
So the code go like this
[self.photoView.layer setCornerRadius:CGRectGetHeight(self.photoView.frame)/2];
[self.photoView.layer setMasksToBounds:YES];
Also works fine ios 6 and above including ios 8.
Upvotes: 1
Reputation: 321
Implement the following code it will solve your problem.
self.imageView.layer.cornerRadius = self.imageView.frame.size.width / 2;
self.imageView.clipsToBounds = YES;
If it is the problem by AutoLayout
. Clear the constraints, put on the spacing and tick on Aspect ratio
.
Upvotes: 0
Reputation: 3914
I am using Xcode6 and and checking with ios8.
Your Imageview's height
and width
should be same.
The below code works fine for me.
yourImageView.layer.cornerRadius = img.frame.size.height /2;
yourImageView.layer.masksToBounds = YES;
yourImageView.layer.borderWidth = 0.1;
Hope it works for you too.
Upvotes: 2
Reputation: 5666
Check your height and width of UIImageView. It should be same. If not them make it. After that
imageView.layer.cornerRadius = imageView.frame.size.width/2;
or
imageView.layer.cornerRadius = imageView.frame.size.height/2;
Both lines work for you.
Upvotes: 3