2017-07-03 68 views
0

你好,我想把UIButton标题标签做成一个圆圈。所以我这样做了。UIButton titleLabel作为圆圈

btnAttachments.setTitle(strcount, for: .normal) 
btnAttachments.setTitleColor(UIColor.white, for: .normal) 
btnAttachments.titleLabel?.backgroundColor=com.getRedcolor() 
btnAttachments.titleLabel?.font=UIFont.init(name: "Bariol", size: 15) 
btnAttachments.titleLabel?.layer.cornerRadius=(btnAttachments.titleLabel?.frame.width)!/2 

但它并没有使它成为一个圆圈。它看起来像这样的代码doen't在titleLabel

UPDATE影响

这是我想要 enter image description here

请帮我 感谢

+0

我想你忘记了btnAttachments.titleLabel.clipsToBounds = YES –

+0

我没有和仍然相同: ( 这是为什么? – user1960169

+0

我认为btnAttachments.titleLabel?.frame.width = 0.我尝试btnAttachments.titleLabel?.layer.cornerRadius = 10.它的工作 –

回答

0

好首先你要什么确认uibutton的clipsToBounds为true,但如果已经设置好了,问题仍然存在,那么你可能试图在“应该”工作的viewDidLoad中实现这一点,但对于某些原因儿子在viewDidLoad中的一些xcode 8版本中布局仍然不是最终的(对象没有设置其真实尺寸),您可以通过在分配角半径之前记录uibutton框架来验证此情况,如果发现某些不连贯的情况可能是这个问题,柜面这是问题,那么你只需要移动你的代码viewWillAppear中

0

你缺少设置MasToBounds

btnAttachments.titleLabel?.layer.masksToBounds = true; 

添加这个,你的代码将正常工作,

你应在0​​或drawRect(通常为UIView类)

我希望这将帮助ü

0

使用下面的代码来获得期望的结果:

let btnAttachments = UIButton(type: .custom) 
btnAttachments.setTitle(strcount, for: .normal).setTitleColor(UIColor.white, for: .normal) 
btnAttachments.frame = CGRect(x: 160, y: 100, width: 50, height: 50) 
btnAttachments.titleLabel?.backgroundColor=com.getRedcolor() 
btnAttachments.titleLabel?.font=UIFont.init(name: "Bariol", size: 15) 
btnAttachments.layer.cornerRadius = 0.5 * button.bounds.size.width 
btnAttachments.clipsToBounds = true