swift中,想要给按钮,变成圆角的。
用了:
1 | var loginButton:UIButton = UIButton(type: UIButtonType.RoundedRect) |
但是没效果,试试默认的直角的:
搜:
swift round circle button
参考:
看到官网:
中说了:RoundedRect在iOS 7之后就废弃了
-》难怪代码没用
最后去用:
1 | loginButton.layer.cornerRadius = 16 |
或:
1 2 3 | loginButton.layer.cornerRadius = 16 loginButton.layer.borderWidth = 2 loginButton.layer.borderColor = UIColor(hexString: "#249ed8" )?.CGColor |
效果:

[总结]
iOS中,用swift设置圆角的button的话:
iOS 7之前,用:
1 | var loginButton:UIButton = UIButton(type: UIButtonType.RoundedRect) |
iOS 7之后用:
1 2 | var loginButton = UIButton() loginButton.layer.cornerRadius = 16 |
如果需要,再去设置,边框的宽度和颜色:
1 2 | loginButton.layer.borderWidth = 2 loginButton.layer.borderColor = UIColor.redColor().CGColor |
转载请注明:在路上 » [已解决]swift实现圆角的按钮