I have a form to collect basic user info. There is a "Continue" button on the form that has different appearances based on whether it is in the .enabled or .disabled state.
The .enabled state formatting works fine with the setTitleColor method. But when I execute setTitleColor for the .disabled state, the button text color changes to a different color than the one I want. What is going on?
I have a CustomButton subclass of UIButton that changes its state to .disabled when the text fields above it are empty. Then I call a function to do some custom formatting to give the .disabled button a medium grey background with a light grey text.
class CustomButton: UIButton {
// Format a button with filled background
// Default button text color to white if no parameter is provided
func configFilledButton(color: UIColor, textColor: UIColor? = UIColor.white) {
self.titleLabel?.textColor = textColor
self.backgroundColor = color
self.setTitleColor(color, for: .disabled)
} // close configFilledButton
// Call this function to toggle the Continue button state to disabled
func toggleEnabledState(isEnabled: Bool) {
if isEnabled == false {
let newColor = UIColor(named: K.BrandColors.greyMedium)!
self.configFilledButton(
color: newColor,
textColor: UIColor(named: K.BrandColors.greyLight))
} // close toggleEnabledState
} // close CustomButton
When I run the code to disable the button, the button background changes to the correct color but the title text changes to a color I did not set (I assume it is using some default color for disabled buttons). I verified that the colors I am using in my Assets folder are correctly named and referenced in my constants K.brandColors structure.
Any idea why setTitleColor does not seem to be working for the .disabled state?
You can save yourself a lot of work by making use of the
.normal/.highlighted/.disabledstates... and then overridingisEnabledto handle the background color change:Here's an example controller... it will create two custom buttons:
.isEnabledstate of the bottom buttonController class:
Looks like this to start:
while the bottom button is tapped (highlighted):
and after tapping the top button to disable the bottom button: