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.
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
}
}
I think there is a typo in the
week1BtnTapped
. TheisSelected
ofbutton2
should befalse
, so that the condition indidTapButton
will work as you want.