如何设置imageView的圆角半径? [英] How to set corner radius of imageView?
问题描述
在Objective-C中这样的行
In Objective-C such line
self.mainImageView.layer.cornerRadius = CGRectGetWidth(self.mainImageView.frame)/4.0f;
完成它的工作,我在Swift中尝试使用类比
does its job, I tried it in Swift using analogy
self.mainImageView.layer.cornerRadius = CGRectGetWidth(self.mainImageView.frame)/4.0
并且它不会改变任何东西,角落和以前一样。而且,Xcode没有显示任何语法错误。 Swift是否支持任何其他方式来实现这一目标?我在这里检查了一些其他线程,通常它是以上面显示的方式在Swift中完成的。
and it doesn't change anything, the corners are the same as before. Moreover, Xcode does not show any syntax errors. Does Swift support any other way to reach this goal? I checked some other threads here and usually it's getting done in Swift in the way showed above.
推荐答案
图层绘制出剪辑区域,需要将其设置为屏蔽边界:
Layer draws out of clip region, you need to set it to mask to bounds:
self.mainImageView.layer.masksToBounds = true
来自 docs :
默认情况下,圆角半径不适用于
图层内容属性中的图像;它仅适用于图层的背景颜色和
边框。但是,将masksToBounds属性设置为
true会导致内容被裁剪为圆角
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. However, setting the masksToBounds property to true causes the content to be clipped to the rounded corners
这篇关于如何设置imageView的圆角半径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!