Alex
Alex

Reputation: 2489

UIButton disappears when round left corner

I want to round only the bottom left corner of a button, but the button disappears from view when I implement the below code.

Code

    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

Upvotes: 0

Views: 198

Answers (1)

Brandon
Brandon

Reputation: 23510

The bounds of your button is most likely zero or close to zero. So the button won't show OR the mask will mask the entire thing.

Upvotes: 1

Related Questions