How to compare colors in swift

后端 未结 4 1741
既然无缘
既然无缘 2020-12-19 08:39

I want change button background for different state. I try so:

 @IBAction func addToShedulerAction(sender: UIButton) {   
       println(sender.backgroundCol         


        
4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-19 09:09

    You don't compare colors using the == operator. You do it like this and you need the ! to unwrap the optional color:

    if sender.backgroundColor!.isEqual(UIColor.redColor()) {
                
    }
    

    Also, remove the extraneous = in your assignment statement. It should be:

    sender.backgroundColor = UIColor.whiteColor()
    

提交回复
热议问题