How to remove CALayer bottom border of UIButton in Swift?

962 Views Asked by At

How to remove CALayer bottom border of UIButton in Swift? Bottom border is added to week1 button but not removed when clicked on button week2.

I am adding screen shot alsoenter image description here

Here is my code:

     @IBAction func week1BtnTapped(_ sender: UIButton) {
            week1.isSelected = true
            didTapButton(btnClicked: week1)
            week2.isSelected = true
            didTapButton(btnClicked: week2)
            btnSelectedText = "Week-1"
            serviceCall()
        }

        @IBAction func week2BtnTapped(_ sender: UIButton) {
            week2.isSelected = true
            week1.isSelected = false
            didTapButton(btnClicked: week1)
            btnSelectedText = "Week-2"
            didTapButton(btnClicked: week2)
            serviceCall()
        }

      func didTapButton(btnClicked:UIButton){
          let border = CALayer()
            if btnClicked.isSelected {
            border.borderColor = UIColor.gray.cgColor
            border.frame = CGRect(x: 2, y: btnClicked.frame.size.height - 2.0, width:  btnClicked.frame.size.width - 5, height: btnClicked.frame.size.height)

            border.borderWidth = 3.0
            btnClicked.layer.addSublayer(border)
            btnClicked.layer.masksToBounds = true
            }
           else{
             border.borderColor = UIColor.white.cgColor
            border.borderWidth = 0.0
            border.frame = CGRect(x: 2, y: btnClicked.frame.size.height , width:  btnClicked.frame.size.width - 5, height: btnClicked.frame.size.height)
            btnClicked.layer.masksToBounds = true
            }
        }
1

There are 1 best solutions below

0
On

I think there is a typo in the week1BtnTapped. The isSelected of button2 should be false, so that the condition in didTapButton will work as you want.