【发布时间】:2020-07-13 10:49:54
【问题描述】:
我创建了一个特殊按钮,如下所示。单击时我可以访问文本值,但是当我更改背景值时,UI 没有任何变化。有谁知道为什么?
class MyButton: UIButton {
override func layoutSubviews() {
super.layoutSubviews()
layer.cornerRadius = 8
titleLabel?.adjustsFontSizeToFitWidth = true
contentHorizontalAlignment = .center
titleLabel?.textColor = .white
backgroundColor = .black
addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector (self.someAction (_:))))
}
@objc func someAction(_ sender:UITapGestureRecognizer) {
print(titleLabel!.text) // text accessible and printed
titleLabel?.textColor = .black //no change
backgroundColor = .white //no change
}
}
【问题讨论】:
标签: ios swift xcode uigesturerecognizer layoutsubviews