Question

I'd like to define a color scheme for my iOS project, in a way that it's easy to replace some colors. Let's say I have a "main" color and a "secondary" color, used in many elements in my app, and in a future I maybe want to set the "main" color to any other value that the one it currently has.

So far I've been using a custom UIColor category to define and use my colors in code, and creating a palette with the same colors for using it in IB and Storyboards.

This way, replacing colors in code is far straightforward, but doing it in IB and Storyboard is so painful... I didn't find an easy way to find/replace colors.

Any idea on how can I manage to do that? I'm open to any suggestion. Thank you in advance

EDIT 1: Maybe it's not clear enough in the question. I'd like to create a scheme such as I can use it both in code and in IB, but defining the colors only once, and being able to switch colors in a way that colors referenced both in code and in IB change accordingly.

Was it helpful?

Solution

An easy way i can do that in Swift:

  • First we have to create as many targets as we want schemes, doing click at actual scheme and select New scheme:

    enter image description here

  • Creating at Assets Catalog Colors Set Colors.xcassets a color like:

    Color at Colors.xcassets

  • Then select one Target membership at Colors.xcassets:

    enter image description here

  • After that create another Colors.xcassets:

    enter image description here

  • Then create again another color with same same at this new Colors.xcassets:

    enter image description here

  • Then select another one Target membership at this new Colors.xcassets:

    enter image description here

  • Then assign color by code using same name referenced (we use PrimaryColor), as in the following example:

    struct Resources {
    
        struct Colors {
            //struct is extended in Colours
        }
    }
    
    extension Colors {
    
        struct Primary {
           static let Default = UIColor(named: "PrimaryColor")!
        }
    }
    
  • And then the implementation:

    newLabel.backgroundColor = Resources.Colors.Primary.Default
    

Finally depending on the scheme chosen, some colors or others will be drawn.

Another way that I have found is next link:

Protocol-Oriented Themes for iOS Apps

OTHER TIPS

You can choose to loop subviews added in the parent view. And can define a set of elements you want to change colors.

So you code might look like below

-(void)defineColors:(UIView *)parent{

    for(UIView *view in parent.subviews){

      if(view.tag == TAG1){
        [self setColor:COLOR1 forControl:view];
      }else if(view.tag == TAG2){
        [self setColor:COLOR2 forControl:view];
      }

    }

}

-(void)setColor:(UIColor *)color forControl:(UIView *)control{

      if([control isKindOfClass:[UILabel class]]){
                [(UILabel *)view setBackgroundColor:color];
      }else if([control isKindOfClass:[UITextField class]]){
                [(UITextField *)view setTextColor:color];
      }

      //So on..


}

I hope it helps.

Cheers.

Code Updated

You can choose to define set of tags for controls you want to assign an specific color, and then set the color accordingly..

Update 2

As per your edited question, you can have a standalone xib in which you can have different small UIView added and you can define colors from xib. Create outlets for different views. And then load the nib in AppDelegate to fetch different colors you want to use you app, and you can save them into a Singleton Class so that you can get them anytime without multiple initializations.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top