UIButton在左转弯时消失

时间:2018-01-28 13:32:35

标签: ios swift uibutton rounded-corners cashapelayer

我想只围绕按钮的左下角,但是当我实现下面的代码时,按钮会从视图中消失。

代码

    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

1 个答案:

答案 0 :(得分:0)

按钮的界限很可能为零或接近于零。因此按钮不会显示或掩码将掩盖整个事物。