I'm not sure if you ever resolved this, but the way I have done something similar is like this.
extension UIControlState {
static let available = UIControlState(rawValue: 1 << 5)
static let pending = UIControlState(rawValue: 1 << 6)
static let waiting = UIControlState(rawValue: 1 << 7)
}
class Button: UIButton {
private var isAvailable = false
private var isPending = false
private var isWaiting = false
private func aFuncCalledWhenPending() {
isPending = true
}
override var state: UIControlState {
var s = super.state
if isAvailable {
s.insert(.available)
}
if isPending {
s.insert(.pending)
}
if isWaiting {
s.insert(.waiting)
}
return s
}
}
This will allow you to write code such as button.setTitleColor(.red, for: .pending)
.
The issue with this approach is your additional states will be publicly visible for all functions accepting the UIControlState
. Similar to how UIControlEvents
has a lot of states but some of them are only used on specific classes.
Keep in mind that if you move forward with this approach, the additional states you include should remain in the same contextual definition of 'control state'.