UIButton 的圆形左边框
Round left border of a UIButton
为了使 UIButton
的左下角变圆,我使用了 并修改了以下行:borderLayer.strokeColor = GenerateShape.UIColorFromHex(0x989898, alpha: (1.0-0.3)).CGColor
b/c 它不起作用。
但是,UIButton
的左边框没有圆角。如何解决?
代码
button.frame = CGRect(x: 0, y: 0, width: 0.5*popUpView.frame.width, height: 0.25*popUpView.frame.height)
button.layer.borderWidth = 3
button.roundBtnCorners(roundedCorners, radius: cornerRadius)
extension UIButton{
// Round specified corners of button
func roundBtnCorners(_ corners:UIRectCorner, radius: CGFloat)
{
let borderLayer = CAShapeLayer()
borderLayer.frame = self.layer.bounds
borderLayer.strokeColor = UIColor(red: 168/266, green: 20/255, blue: 20/255, alpha: 0.7).cgColor
borderLayer.fillColor = UIColor.clear.cgColor
borderLayer.lineWidth = 1.0
let path = UIBezierPath(roundedRect: self.bounds,
byRoundingCorners: corners,
cornerRadii: CGSize(width: radius, height: radius))
borderLayer.path = path.cgPath
self.layer.addSublayer(borderLayer);
}
}
而不是:
self.layer.addSublayer(borderLayer)
尝试:
self.layer.mask = borderLayer
我看不出你是如何为 roundBtnCorners 创建参数的,但下面是我在你的代码中更改的行,它起作用了。我会检查以确保您传递的参数正确。
button.roundBtnCorners(.bottomLeft , radius: 10)
解决了!那是因为我的button.layer.borderWidth = 3
画了一个很粗的矩形,所以我看不到roundBtnCorners
画的边框
我删除了下面这行:
button.layer.borderWidth = 3
结果:
为了使 UIButton
的左下角变圆,我使用了 borderLayer.strokeColor = GenerateShape.UIColorFromHex(0x989898, alpha: (1.0-0.3)).CGColor
b/c 它不起作用。
但是,UIButton
的左边框没有圆角。如何解决?
代码
button.frame = CGRect(x: 0, y: 0, width: 0.5*popUpView.frame.width, height: 0.25*popUpView.frame.height)
button.layer.borderWidth = 3
button.roundBtnCorners(roundedCorners, radius: cornerRadius)
extension UIButton{
// Round specified corners of button
func roundBtnCorners(_ corners:UIRectCorner, radius: CGFloat)
{
let borderLayer = CAShapeLayer()
borderLayer.frame = self.layer.bounds
borderLayer.strokeColor = UIColor(red: 168/266, green: 20/255, blue: 20/255, alpha: 0.7).cgColor
borderLayer.fillColor = UIColor.clear.cgColor
borderLayer.lineWidth = 1.0
let path = UIBezierPath(roundedRect: self.bounds,
byRoundingCorners: corners,
cornerRadii: CGSize(width: radius, height: radius))
borderLayer.path = path.cgPath
self.layer.addSublayer(borderLayer);
}
}
而不是:
self.layer.addSublayer(borderLayer)
尝试:
self.layer.mask = borderLayer
我看不出你是如何为 roundBtnCorners 创建参数的,但下面是我在你的代码中更改的行,它起作用了。我会检查以确保您传递的参数正确。
button.roundBtnCorners(.bottomLeft , radius: 10)
解决了!那是因为我的button.layer.borderWidth = 3
画了一个很粗的矩形,所以我看不到roundBtnCorners
我删除了下面这行:
button.layer.borderWidth = 3
结果: