如何在Swift中删除UIButton的CALayer底部边框?

时间:2016-12-19 08:57:21

标签: swift uibutton calayer

如何在Swift中删除UIButton的CALayer底部边框?底部边框添加到第1周按钮,但单击第2周按钮时不会删除。

我还添加了屏幕截图enter image description here

这是我的代码:

     @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 个答案:

答案 0 :(得分:0)

我认为week1BtnTapped中存在拼写错误。 isSelected的{​​{1}}应为button2,以便false中的条件可以按您的意愿运行。