iOS button title color won't change

久未见 提交于 2019-12-10 14:47:13

问题


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

}

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



回答2:


for swift 3

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


来源:https://stackoverflow.com/questions/36831596/ios-button-title-color-wont-change

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!