【发布时间】:2022-01-02 09:27:41
【问题描述】:
我在故事板中内置的一系列按钮中看到了一些奇怪的行为。我有 4 个自定义类型的按钮 TakesContainerButton,当单击一个按钮时,它会更改为系统字体,但是当单击一个不同的按钮时,前一个按钮会返回到所需的字体,不确定这里发生了什么
按钮也嵌入在堆栈视图中,如果这很重要的话
这是按下其中一个按钮时的实现,其中buttons 是 4 个按钮的数组
@IBAction func filterPressed(_ sender: TakesContainerButton) {
for button in buttons {
button.unclick()
}
sender.click()
}
这是自定义类
class TakesContainerButton: UIButton {
var bottom = UIView()
func click(){
self.setTitleColor(.darkGray, for: .normal)
let xOffset:CGFloat = 10
bottom = UIView(frame: CGRect(x: xOffset / 2, y: self.frame.height - 3, width: self.frame.width - xOffset, height: 3))
bottom.layer.cornerRadius = 1.5
bottom.backgroundColor = .darkGray
self.addSubview(bottom)
}
func unclick(){
bottom.removeFromSuperview()
self.setTitleColor(UIColor(hex: "8B8B8B"), for: .normal)
}
override func awakeFromNib(){
setFont()
}
func setFont(){
self.titleLabel?.font = UIFont(name: "Lato-Bold", size: 12)
}
}
【问题讨论】: