I've my own custom button implemented and this works well.
import Foundation
import UIKit
class GhostYouButton: UIButton {
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override var isEnabled: Bool {
didSet {
if (self.isEnabled == false) {
self.backgroundColor = UIColor.clear
self.titleLabel?.textColor = Constant.disabledGrayColor
self.tintColor = Constant.disabledGrayColor
self.borderColor = Constant.disabledGrayColor
self.borderWidth = 2
self.cornerRadius = 20
} else {
self.backgroundColor = UIColor.clear
self.titleLabel?.textColor = Constant.mainGreenColor
self.tintColor = Constant.mainGreenColor
self.borderColor = Constant.mainGreenColor
self.borderWidth = 2
self.cornerRadius = 20
}
}
}
}
I set my GhostYouButton to be disabled in the viewDidLoad():
override func viewDidLoad() {
self.nextButton.isEnabled = false
}
So it turns gray like I expect it to:
However... as you can see the title on the UIButton is faded out. I want this to be the exact same color as the border. How do I make this happen?
Use this line
if (self.isEnabled == false){
:
self.setTitleColor(UIColor.gray, for: .normal)
:
}
else{
:
self.setTitleColor(UIColor.green, for: . disabled)
:
}
Right now, it looks like you are setting the title color of the label directly. Although this works, it is not the best way to set the title color when working with a UIButton as (as you are experiencing) this property can change depending on the state of the button. Instead, you should use the setTitleColor:forState:
method of UIButton which gives you more fine grain control over the style and the appearance of the button as the button stage changes: https://developer.apple.com/reference/uikit/uibutton/1623993-settitlecolor?language=swift
With this method, you can pass in a control state such as disabled, highlighted, etc: https://developer.apple.com/reference/uikit/uicontrolstate
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.