如何设置UIButton图像,该图像是带有边框的圆形 [英] How can I set UIButton image, which Image is round shape with border
问题描述
如何设置UIButton图像,该图像是带有边框的圆形,我编写了相关代码,但是图像没有以正确的圆形显示. 这是我的代码.
How can I set UIButton image, which Image is round shape with border I write related code but image not displaying in proper round shape.. Here is my code..
btnMyProfile.setImage(UIImage(named: "MyDefaultImg.png"), for: .normal)
btnMyProfile.imageView?.contentMode = .scaleAspectFit
btnMyProfile.imageView?.layer.cornerRadius = (btnMyProfile.imageView?.frame.width)! / 2
btnMyProfile.imageView?.layer.borderColor = UIColor.blue.cgColor
btnMyProfile.imageView?.layer.borderWidth = 2.0
我的o/p看起来像这样.
My o/p look like this..
但是我需要这样.
我该怎么做才能达到所需的输出?请帮助我..谢谢高级..
What can I do for achieve my desired output? Please help me.. Thanks in advanced..
推荐答案
为图像视图设置clipsToBounds = true
btnMyProfile.imageView.clipsToBounds = true
或使用
btnMyProfile.imageView.layer.masksToBounds = true
从苹果文档:
默认情况下,拐角半径不适用于 图层的内容属性;它仅适用于背景色和 图层的边框.但是,将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.
其他选择使用 查看全文