'iOS button title color won't change

I'm creating a UIButton dynamically with the following code which creates it as per specified style.

let frame = CGRect(x: 10, y: 6, width: 60, height: 30 )    
let button = UIButton(frame: frame)
button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
button.backgroundColor = UIColor.whiteColor()
button.addTarget(self, action: "filterByCategory:", forControlEvents: UIControlEvents.TouchUpInside)
self.categoryScrollView.addSubview(button)

With this button, I want to toggle the style when tapped. The following code changes the background color but not the title color. Any idea why title color won't change?

func filterByCategory(sender:UIButton) {

    if sender.backgroundColor != UIColor.blackColor() {
        sender.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Selected)
        sender.backgroundColor = UIColor.blackColor()
    } else {
        sender.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        sender.backgroundColor = UIColor.whiteColor()
    }

}


Solution 1:[1]

Because your button will turn back to UIControlState.Normal after you touch it, it become .Highlighted, then .Normal

You should set sender.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Selected)

to

sender.setTitleColor(UIColor.whiteColor(), forState: UIControlState. Normal)

or, just set it for all the states since u did a check for color like

sender.setTitleColor(UIColor.whiteColor(), forState: [.Normal,.Selected,.Highlighted])

*Edit: Above doesn't work, can use NSAttributedString instead

if self.loginButton.backgroundColor == UIColor.blackColor() {
            let tittle = NSAttributedString(string: "Login", attributes: [NSForegroundColorAttributeName: UIColor.blackColor()])
            loginButton.setAttributedTitle(tittle, forState: .Normal)
            loginButton.backgroundColor = UIColor.whiteColor()
        } else {
            let tittle = NSAttributedString(string: "Login", attributes: [NSForegroundColorAttributeName: UIColor.whiteColor()])
            loginButton.setAttributedTitle(tittle, forState: .Normal)
            loginButton.backgroundColor = UIColor.blackColor()
        }

Solution 2:[2]

for swift 3

button.setTitleColor(.black, for: .normal)

Solution 3:[3]

From the above described problem. I came to a conclusion that the button you created doesn't have a title, so that's why you're not able to see changes in the title colour.

To resolve the issue:-

  1. set title for the button
  2. change title colour using UIButton property button.setTitleColor(.black, for: .normal)

Solution 4:[4]

I have find solutions for more than 4 to 5 hours but did not found anything and concluded this two points :-

  • If you are giving title from storyboard and setting the title color from programmatically or custom class than it will no work. Solution for this is
  1. You can set the title programmatically and change its color programmatically using simple setTitleColor() method.
  2. Or else you can use storyboard for both setting the title and setting the color through foreground color.
  3. And if you are using Custom Class that you can use Inspectable and set its value through programmatically and set the color for the title using solution 1.
  • If you do not want to do this steps just change the button type to default and it will work in any ways you are setting the titleColor either programmatically or from storyboard

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 ricks
Solution 3 Pathak Ayush
Solution 4 Shubham Vyas