Seong Lee
Seong Lee

Reputation: 10530

How to check backgroundColor of all buttons

I want to be able to check if all buttons' background color is UIColor.whiteColor. I'm determining whether a view is in search mode or in normal mode by the button state.

I want to do something similar with the following but contains() checks if array contains certain value. It wouldn't work in my case because UIColor.whiteColor is a property of UIButton.

if contains(categoryScrollView.subviews, UIColor.whiteColor) {
    inSearchMode = false 
}

Then if I put it in the following way, I do not know how I can make sure all button's background color is white as it will pass validation as soon as any button's background color is white which is not what I need.

for button in categoryScrollView.subviews {

    if button.backgroundColor == UIColor.whiteColor() {
        inSearchMode = false                      
    }
}

How can I check is background color of all buttons?

Upvotes: 0

Views: 515

Answers (4)

Kalaivani
Kalaivani

Reputation: 424

Add a counter like whiteBtnCount in the loop where you are checking the backgroundcolor.Increment that counter if it matches the color and break the loop once counter reaches the button count. Voila,now you know whether all buttons are white color or not.

var whiteBtnCount: Int = 0
for button in categoryScrollView.subviews {

    if button.backgroundColor == UIColor.whiteColor() {
       whiteBtnCount += 1
         if whiteBtnCount == btnCount { //ensure btnCount variable holds the number of buttons
          inSearchMode = false
          break
       }                      
    }
}

Upvotes: 0

yusuke024
yusuke024

Reputation: 2229

var allWhite = true
for button in categoryScrollView.subviews {
    if button.backgroundColor != UIColor.whiteColor() {
        allWhite = false                      
    }
}
inSearchMode = !allWhite 

But IMHO, this is not a good way to do it at all. You should have a code to do state transition and make the buttons white or not white based on this state.

Upvotes: 2

Chajmz
Chajmz

Reputation: 749

If you don't have a lot of UIButtons and that they are already declared as IBOutlets you can create a function that loops through all your UIButtons :

for button in [yourButton1, yourButton2, yourButton2] {
  if button.backgroundColor == UIColor.whiteColor {
   // Do something 
  } else {
  // Do something else 
  }
}

Or you can also loop through all the buttons of your self.view :

for view in self.view.subviews as [UIView] {
    if let button = view as? UIButton {
        if button.backgroundColor == UIColor.whiteColor {
        // Do something 
        } else {
         // Do something else 
       } 
    }
}

Upvotes: 0

sschale
sschale

Reputation: 5188

I'd enclose this check in a function, like this (note that I'm including a check that each view is a button):

func allWhiteButtons(view: UIView)-> Bool{
  for view in view.subViews {
    if let button = view as? UIButton {
      if button.backgroundColor != UIColor.whiteColor() {
        return false
      }
    }
  }
  return true
}

Upvotes: 2

Related Questions