Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS button title color won't change

Tags:

ios

swift

xcode7

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

}
like image 275
Seong Lee Avatar asked Apr 25 '16 03:04

Seong Lee


2 Answers

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()
        }
like image 91
Tj3n Avatar answered Sep 27 '22 20:09

Tj3n


for swift 3

button.setTitleColor(.black, for: .normal)
like image 44
ricks Avatar answered Sep 27 '22 22:09

ricks