圆形 ImageView iOS 8 / Xcode 6

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27287298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:18:41  来源:igfitidea点击:

Circular ImageView iOS 8 / Xcode 6

iosobjective-cxcode

提问by user3796045

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.cornerRadiusproperty. 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?

我已经四处搜索,但似乎仍然无法解决这个问题。我正在尝试在 iOS 8 中实现圆形图像。在 Xcode 6 之前,我认为答案是imageView.layer.cornerRadius财产。我已经尝试过这个,但是在 Xcode 6 和 iOS 8 中现在使用自动布局,我认为这是导致问题的原因,它似乎无法正常工作。形状不是圆形,而是一些眼睛形状或变形的椭圆形,它永远不会是一个完美的圆形。是否还有其他方法可以通过 Autolayout 实现这一目标?

回答by Indrajeet

Check your height and width of UIImageView. It should be same. If not them make it. After that

检查 UIImageView 的高度和宽度。应该是一样的。如果不是他们成功。在那之后

imageView.layer.cornerRadius = imageView.frame.size.width/2;

or

或者

imageView.layer.cornerRadius = imageView.frame.size.height/2;

Both lines work for you.

两条线都适合你。

回答by Manthan

I am using Xcode6 and and checking with ios8.

我正在使用 Xcode6 并使用 ios8 进行检查。

Your Imageview's heightand widthshould be same.

你的 Imageviewheightwidth应该是一样的。

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.

希望它也适用于你。

回答by Sudhin Davis

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.

也适用于 ios 6 及更高版本,包括 ios 8。

回答by Hem Poudyal

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.

如果是问题由AutoLayout。清除约束,设置间距并勾选Aspect ratio