xcode 故事板和自动布局:如何制作圆形图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28182206/
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
Storyboard and autolayout: how make a circular image
提问by Tom
in storyboard (xcode 6) i want a circular user image profile take from Facebook.
在故事板(xcode 6)中,我想要从 Facebook 获取的圆形用户图像配置文件。
So i have make this interface in storyboard, using auto layout:
所以我在故事板中制作了这个界面,使用自动布局:
Then, using Facebook iOS sdk i take the user profile (using swift):
然后,使用 Facebook iOS sdk 获取用户个人资料(使用 swift):
var facebookProfileUrl = "http://graph.facebook.com/\(userId!)/picture?type=normal";
In storyboard i have set the image to "Scale to fit" mode. To make the image view circular i use the following code:
在故事板中,我已将图像设置为“缩放以适合”模式。为了使图像视图循环,我使用以下代码:
self.facebookProfileImage.layer.cornerRadius = self.facebookProfileImage.frame.size.width / 2;
self.facebookProfileImage.clipsToBounds = true;
When i run the code, anyway the image doesn't look circular:
当我运行代码时,无论如何图像看起来不是圆形的:
I suppose the problem is auto layout but i'm not sure. How can i make the image perfectly circular??
我想问题是自动布局,但我不确定。我怎样才能使图像完美圆形?
回答by s.ka
Two steps:
两步:
- Center the UIImageView by adding a "Horizontal Center In Container" constraint (Editor > Align > Horizontal Center in Container) to the UIImageView.
- Remove the leading and trailing constraints you currently have set on the UIImageView.
- 通过向 UIImageView 添加“容器中的水平居中”约束(编辑器 > 对齐 > 容器中的水平居中)使 UIImageView 居中。
- 删除您当前在 UIImageView 上设置的前导和尾随约束。
Why? The UIImageView is getting stretched because Auto Layout needs to account for the leading and trailing constraints you set on the UIImageView. To prove my point, set the priority of the leading and trailing constraints to something less than the priority of the height and width constraints. You should see a rounded image like you expect, but it may not be centered.
为什么?UIImageView 正在被拉伸,因为自动布局需要考虑您在 UIImageView 上设置的前导和尾随约束。为了证明我的观点,将前导和尾随约束的优先级设置为小于高度和宽度约束的优先级。您应该会像预期的那样看到圆形图像,但它可能没有居中。
回答by Y_Y
More steps:
更多步骤:
- Add aspect ratio constraint 1:1
- mark check clip to boundsattribute in attribute inspector
- make outlet of your view into you controller class
set corner radiusto half of either its height or width
yourImageViewOutlet.layer.cornerRadius = yourImageViewOutlet.frame.size.width / 2.0
- 添加纵横比约束 1:1
- 在属性检查器中标记检查剪辑到边界属性
- 将您的视图输出到您的控制器类中
将角半径设置为其高度或宽度的一半
yourImageViewOutlet.layer.cornerRadius = yourImageViewOutlet.frame.size.width / 2.0
回答by Ramaraj T
You have given leading constraint, trailing constraint
and the width constraint.
So the image will try to leave 130 pixels before and after the image which will increase the width of the image.
您已经给出leading constraint, trailing constraint
并且width constraint.
因此图像将尝试在图像前后留下 130 像素,这将增加图像的宽度。
So the solution is, remove either one of the leading or trailing constraint.
The best workaround is, remove both the constraint and add a horizontal centre constraint, that is what you want.
最好的解决方法是,删除约束并添加水平中心约束,这就是您想要的。
回答by SPatel
SWIFT 3.x Just change your imageView custom class with this and enjoy.
SWIFT 3.x 只需更改您的 imageView 自定义类即可享受。
@IBDesignable class RoundedImageView:UIImageView {
@IBInspectable var borderColor:UIColor = UIColor.white {
willSet {
layer.borderColor = newValue.cgColor
}
}
override func layoutSubviews() {
super.layoutSubviews()
layer.cornerRadius = frame.height/2
layer.masksToBounds = true
layer.borderWidth = 1
layer.borderColor = borderColor.cgColor
}
}
回答by raistlin
I have made the same thing a little time ago and this worked for me
不久前我做了同样的事情,这对我有用
self.imageView.image = [ImageHelper getImage]; //retrieve image
self.imageView.layer.cornerRadius = self.imageView.frame.size.height / 2;
self.imageView.layer.masksToBounds = YES;
self.imageView.layer.borderWidth = 0;
self.imageView.contentMode = UIViewContentModeScaleAspectFill;
回答by Faiz Mokhtar
When adding the constraint, just make sure that you check the height and width so that it will be fix. At least that what I always do.
添加约束时,只需确保检查高度和宽度,以便修复。至少我一直都是这样做的。
回答by matt
In storyboard i have set the image to "Scale to fit" mode
在故事板中,我已将图像设置为“缩放以适合”模式
But that's a problem, isn't it? It means: "Stretch the image so that it matches the way the image view is stretched." If that isn't what you want, don't say that! Use Centered, or at least use one of the content modes with "Aspect" in its name so that your image is not stretched.
但这是个问题,不是吗?它的意思是:“拉伸图像,使其与图像视图的拉伸方式相匹配。” 如果那不是您想要的,请不要那样说!使用居中,或者至少使用名称中带有“Aspect”的内容模式之一,这样您的图像就不会被拉伸。
As for the circle itself, setting the cornerRadius
is no way to make a circle. The way to create a circular boundary around an image is to maskthe image. You can redraw the image with the circular mask as a clipping boundary, or you can apply the circular mask to the image view. (See, for example, my answer here: https://stackoverflow.com/a/16475824/341994.)
至于圆本身,设置cornerRadius
不是做圆的方法。在图像周围创建圆形边界的方法是对图像进行遮罩。您可以使用圆形蒙版作为剪切边界重新绘制图像,也可以将圆形蒙版应用于图像视图。(例如,参见我的回答:https: //stackoverflow.com/a/16475824/341994。)
It is true that your image view is also being stretched, because you gave it constraints to both sides of the superview. You can prevent that by giving it a width constraint instead; now its width will be absolute. But you should stilldo the right thing on the other two issues.
确实,您的图像视图也被拉伸,因为您将其约束到超级视图的两侧。你可以通过给它一个宽度约束来防止这种情况发生;现在它的宽度将是绝对的。但是你仍然应该在其他两个问题上做正确的事情。