Reputation: 42690
For some reason, I want to define my own tint color, when an UIButton
is disabled.
I thought it is able to achieve so based on
https://stackoverflow.com/a/45835079/72437
import UIKit
class UIButtonEX: UIButton {
override var isEnabled: Bool {
didSet{
if self.isEnabled {
self.tintColor = UIColor.red
}
else{
self.tintColor = UIColor.red
}
}
}
}
class ViewController: UIViewController {
@IBOutlet weak var button: UIButtonEX!
@IBOutlet weak var disabledButton: UIButtonEX!
override func viewDidLoad() {
super.viewDidLoad()
button.isEnabled = true
disabledButton.isEnabled = false
//button.tintColor = .red
//disabledButton.tintColor = .red
}
}
The right button is disabled UIButton
. I wish it would stay in red even it is disabled.
However, it is still painted as default system light grey.
May I know, what steps I have missed out? Is it possible to define tint color for a disabled UIButton?
Thanks.
Upvotes: 0
Views: 299
Reputation: 1342
You may have to try something like this, and this works when you are using the system named images.
class UIButtonEX: UIButton {
override func tintColorDidChange() {
if tintAdjustmentMode == .dimmed {
// modify subviews to look disabled
// In your case tou have to heep as it is
tintAdjustmentMode = .normal
} else {
// modify subviews to look enabled
}
}
}
Upvotes: 1