我发现圆形 UIButton 存在一个奇怪的问题。
这是我创建此按钮的代码块。
let roundedButton = UIButton(type: .System)
roundedButton.frame = CGRectMake(100, 100, 100, 100)
roundedButton.backgroundColor = UIColor.blackColor()
roundedButton.layer.borderColor = UIColor.whiteColor().CGColor
roundedButton.layer.borderWidth = 3.0
roundedButton.layer.cornerRadius = roundedButton.frame.size.width/2
roundedButton.layer.masksToBounds = true
self.view.addSubview(roundedButton)
As you can see, there's a UIButton with backgroundColor, border and corner radius. It's fully rounded. But in the output I get the next appearance:
![rounded button with 1px border](https://i.stack.imgur.com/E6jVa.png)
你可以看到按钮外面 1px 的边框,它是它的背景色(黑色)。似乎它的内边框(白色)不是从按钮的边缘开始的。
您知道如何解决这个问题吗?
使用 CAShapeLayer 代替,它还可以提供增强的性能:
let roundedButton = UIButton(type: .Custom)
roundedButton.frame = CGRectMake(100, 100, 100, 100)
let _border = CAShapeLayer()
_border.path = UIBezierPath(roundedRect: roundedButton.bounds, cornerRadius:roundedButton.frame.size.width/2).CGPath
_border.frame = roundedButton.bounds
_border.strokeColor = UIColor.whiteColor().CGColor
_border.fillColor = UIColor.blackColor().CGColor
_border.lineWidth = 3.0
roundedButton.layer.addSublayer(_border)
self.view.addSubview(roundedButton)
记住不要使用roundedButton的backgroundColor,只使用CAShapeLayer的fillColor。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)