[英]UIButton custom background color highlighted and disabled
I have button which is using class customButton. 我有使用class customButton的按钮。 This button function is after 10 touches automatically disable itself. 十次触摸后,此按钮功能会自动禁用。
I have issue with it, on last touch it will take color from isHighlighted and not from isEnabled. 我对此有疑问,在最后一次触摸时,它将从isHighlighted中获取颜色,而不是从isEnabled中获取颜色。 How would you fix this? 您将如何解决?
Many thanks. 非常感谢。
class customButton: UIButton {
override open var isHighlighted: Bool {
didSet {
//Set colors for Highlighted & Unhighlighted
backgroundColor = isHighlighted ? UIColor(named: "RedAlpha")! : UIColor(named: "Red")!
}
}
override open var isEnabled: Bool {
didSet {
backgroundColor = isEnabled ? UIColor(named: "Red")! : UIColor(named: "Dis")!
}
}
}
Depending on the last state change of the button it could have different colour. 根据按钮的最后状态变化,它可以具有不同的颜色。 You can try this. 你可以试试看
class customButton: UIButton {
override open var isHighlighted: Bool {
didSet {
updateBackgroundColor()
}
}
override open var isEnabled: Bool {
didSet {
updateBackgroundColor()
}
}
func updateBackgroundColor() {
//Set colors for Highlighted & Unhighlighted
if isEnabled {
backgroundColor = isHighlighted ? UIColor(named: "RedAlpha")! : UIColor(named: "Red")!
} else {
backgroundColor = UIColor(named: "Dis")!
}
}
}
As far as I understood you problem, proper way would be to change the colour of isEnabled
. 据我了解您的问题,正确的方法是更改isEnabled
的颜色。 But if you want what you've written in question then on last touch set isHighlighted
to true
and set isUserInteractionEnabled
to false
so that it'll ignore user events. 但是,如果您要写的是有问题的内容,则在最后一次触摸时将isHighlighted
设置为true
,并将isUserInteractionEnabled
设置为false
这样它将忽略用户事件。
customButton.isHighlighted = true
customButton.isUserInteractionEnabled = false
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.