【问题标题】:UIButton disappears when round left corner左圆角时UIButton消失
【发布时间】:2018-07-07 07:12:47
【问题描述】:

我只想对按钮的左下角进行圆角处理,但是当我实现以下代码时,按钮会从视图中消失。

代码

    cancelBtn.setTitle("Cancel", for: .normal)
    cancelBtn.setTitleColor(UIColor.ecaftRed, for: .normal)
    cancelBtn.layer.borderWidth = 3
    cancelBtn.layer.borderColor = UIColor.whiteFadedPlus.cgColor
    cancelBtn.roundBtnCorners([.bottomLeft], radius: 25)
    cancelBtn.addTarget(self, action: #selector(cancelBtnTapped(_:)), for: .touchUpInside)
    popUpView.addSubview(cancelBtn)

    cancelBtn.snp.makeConstraints { (make) -> Void in
        make.left.equalTo(popUpView)
        make.bottom.equalTo(popUpView)
        make.size.equalTo(CGSize(width: 0.5*popUpView.frame.width, height: 0.25*popUpView.frame.height))
    }


extension UIButton{

    func roundBtnCorners(_ corners:UIRectCorner, radius: CGFloat){
        let path = UIBezierPath(roundedRect: self.bounds,
                                     byRoundingCorners: corners,
                                     cornerRadii: CGSize(width: radius, height: radius))
        let maskLayer = CAShapeLayer()
        maskLayer.frame = self.bounds
        maskLayer.path = path.cgPath
        self.layer.mask = maskLayer
    }
}

Source

【问题讨论】:

  • 我试过你的代码,它对我有用.. 显示你的cancelBtn 代码..
  • 你的代码好像可以工作
  • 按钮的边界可能为零。
  • @Brandon 我更新了框架,谢谢!
  • @Brandon 你想把它作为答案发布吗?

标签: ios swift uibutton rounded-corners cashapelayer


【解决方案1】:

按钮的边界很可能为零或接近于零。所以按钮不会显示或者面具会掩盖整个事情。

【讨论】:

    猜你喜欢
    • 2015-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-29
    • 2016-09-06
    相关资源
    最近更新 更多