iosswiftxcode7

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

  • 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()
            }