gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:49:13 25 4
gpt4 key购买 nike

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

我还添加了屏幕截图 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
}
}

最佳答案

我认为week1BtnTapped中有一个拼写错误。 isSelectedbutton2应该是false ,这样didTapButton中的条件将按照您的意愿工作。

关于swift - 如何在 Swift 中删除 UIButton 的 CALayer 底部边框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41218912/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com