11

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

 @IBAction func addToShedulerAction(sender: UIButton) {   
       println(sender.backgroundColor)     
        if sender.backgroundColor==UIColor.redColor(){
            sender.backgroundColor==UIColor.whiteColor()
        }
        else //if sender.backgroundColor==UIColor.whiteColor()
        {
            sender.backgroundColor=UIColor.redColor()
        }
    }

but in first push button println print nil and background change to red, in second push println print "Optional(UIDeviceRGBColorSpace 1 0 0 1)" and color doesn't change

Walter West
  • 829
  • 3
  • 12
  • 31
  • Take a look at this question: http://stackoverflow.com/questions/970475/how-to-compare-uicolors – Keenle Dec 19 '14 at 05:06

4 Answers4

24

Previous answers are wrong cause

UIColor.black.isEqual(UIColor(red: 0, green: 0, blue: 0, alpha: 1))

returns false

add this code to your project (Swift 4)

extension UIColor {
  static func == (l: UIColor, r: UIColor) -> Bool {
    var r1: CGFloat = 0
    var g1: CGFloat = 0
    var b1: CGFloat = 0
    var a1: CGFloat = 0
    l.getRed(&r1, green: &g1, blue: &b1, alpha: &a1)
    var r2: CGFloat = 0
    var g2: CGFloat = 0
    var b2: CGFloat = 0
    var a2: CGFloat = 0
    r.getRed(&r2, green: &g2, blue: &b2, alpha: &a2)
    return r1 == r2 && g1 == g2 && b1 == b2 && a1 == a2
  }
}
func == (l: UIColor?, r: UIColor?) -> Bool {
  let l = l ?? .clear
  let r = r ?? .clear
  return l == r
}

so now

UIColor.black == UIColor(red: 0, green: 0, blue: 0, alpha: 1)

returns true

and for you:

if sender.backgroundColor == .red {
  sender.backgroundColor = .white
} else {
  sender.backgroundColor = .red
}

now your code looks pretty :)

nbloqs
  • 3,152
  • 1
  • 28
  • 49
Dmitry Kozlov
  • 1,115
  • 10
  • 14
  • 1
    Comparing CGFloats with == is not a good idea. In floating point values it is better to use inequalities or a subtraction with a threshold (delta). – nbloqs Apr 18 '18 at 13:31
  • Note that this only changes the behaviour of `==`, not `!=`, so you'll need to either implement `!=` as well, or remember to always use `!(color1 == color2)` rather than `color1 != color2` – Angela May 18 '20 at 18:11
16

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()
Ian
  • 12,538
  • 5
  • 43
  • 62
1

Remember when you are comparing UIColors you have to write description at the end of the color for both side, if you don't write this, every time it will enter into else block. For Example, see the below code

if(ocularPlus1Btn.backgroundColor?.description == UIColor.init(red: 23.0 / 255.0, green: 82 / 255.0, blue: 84 / 255.0, alpha: 1).description) {
   return "positive"
} else {
   return "negative"
}
suresh
  • 55
  • 3
-2

You have a mistake in your first if clause. It should be:

if sender.backgroundColor.isEqual(UIColor.redColor()){
    sender.backgroundColor=UIColor.whiteColor // only one '=' here
}
tng
  • 4,286
  • 5
  • 21
  • 30