如何设置 UIButton 图像,该图像是带边框的圆形

How can I set UIButton image, which Image is round shape with border(如何设置 UIButton 图像,该图像是带边框的圆形)
本文介绍了如何设置 UIButton 图像,该图像是带边框的圆形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何设置 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?

推荐答案

为你的 imageview 设置 clipsToBounds = true

set clipsToBounds = true for your imageview

btnMyProfile.imageView.clipsToBounds = true

或使用

btnMyProfile.imageView.layer.masksToBounds = true

来自 苹果文档:

默认情况下,圆角半径不适用于图层的内容属性;它仅适用于背景颜色和层的边界.但是,将 maskToBounds 属性设置为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.

其他选项使用 UIBezierPath

这篇关于如何设置 UIButton 图像,该图像是带边框的圆形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
Dropbox Files.download does not start when number of files in folder is gt; 1000(当文件夹中的文件数为1000时,Dropbox Files.Download不会启动)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)