【问题标题】:Round left border of a UIButtonUIButton 的圆形左边框
【发布时间】:2018-01-28 16:26:14
【问题描述】:

为了使UIButton 的左下角变圆,我使用了code from this answer 并修改了这一行: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);
    }
}

【问题讨论】:

  • 为什么不直接改变 layer.cornerRadius 属性呢?现在行为是正常的,你的 UIButton 必须被裁剪到 superView 的边界,并且没有在 UIButton 上应用任何cornerRadius。
  • @SwiftRabbit 我只想圆 UIButton 的左下角,这就是原因

标签: ios swift uibutton uibezierpath


【解决方案1】:

代替:

self.layer.addSublayer(borderLayer)

试试:

self.layer.mask = borderLayer

【讨论】:

  • 没用:/ - 它导致按钮的文本消失 + 边框很薄并且没有出现在圆角处
【解决方案2】:

我看不到您是如何为roundBtnCorners 创建参数的,但下面是我在您的代码中更改的行并且它有效。我会检查以确保您传递了正确的参数。

    button.roundBtnCorners(.bottomLeft , radius: 10)

【讨论】:

    【解决方案3】:

    解决了!因为我的button.layer.borderWidth = 3画了一个很粗的矩形,所以看不到roundBtnCorners画的边框

    我删除了下面的行:

    button.layer.borderWidth = 3
    

    结果:

    【讨论】:

      猜你喜欢
      • 2017-04-05
      • 2011-02-03
      • 1970-01-01
      • 2016-09-28
      • 1970-01-01
      • 2016-09-26
      • 2013-10-14
      • 2010-10-14
      • 1970-01-01
      相关资源
      最近更新 更多