views:

222

answers:

2

Hello,

I am wondering if it is possible to change a default color of buttons in my application with a couple lines of code? It may be really boring to manually change color of each button. Especially if you have lots of them.

For example, I want all of my buttons to be orange instead of original white.

Thank you in advance, Ilya.

+3  A: 

I'm afraid there is no global setting.

It is possible to iterate through all the view and subviews of an object, query their type, and then apply custom colors as needed. This allows you to have one place in your application that you pass your views to and everything is applied.

Something like;

//! Applies application specific styles to UIKit objects
void applyStyles(UIView* view)
{
  for(UIView* subview in view.subviews)
  {
    // test type of view
    if([[subview class] isEqualToString:@"UIButton"] == YES)
    {
      // apply colors for buttons
    }
    else if([[subview class] isEqualToString:@"SomethingElse"] == YES)
    {
      // apply colors for something else
    }
  }
}
Andrew Grant
I think those tests should be `[subview isKindOfClass:[UIButton class]]`
benzado
+1  A: 

I would recommend creating a subclass of UIButton (MyCustomButton) and applying whatever stylistic elements you want within the constructor of your subclass.

Passing UIButtons to some sort of "StyleManager" as suggested above is a better approach than saying

button.backgroundColor = [UIColor orangeColor];

but you still have to go through your code and say something like

[StyleManager applyStyles:viewThatContainsButton];

This spreads the solution of applying styles to probably every Controller and some View elements within your code base. Creating a subclass lets you apply the style with one line of code, not one line per view.

Collecting the solution to your problem into one class eliminates this solution sprawl and should reduce the maintenance effort as well.

Jonathan Arbogast