How to define color scheme in Xcode

I would like to define a color scheme for my iOS project to easily replace some colors. Say I have a “primary” color and a “secondary” color used in many elements of my application, and in the future I might want to set a “primary” color for any other value that it currently has.

So far, I have used the UIColor custom category to define and use my colors in code and create a palette with the same colors for use in IB and storyboards.

Thus, replacing colors in code is very simple, but doing it in IB and Storyboard is so painful ... I have not found an easy way to find / replace colors.

Any idea on how I can do this? I am open to any suggestion. Thank you in advance

EDIT 1: Perhaps this issue is not clear enough. I would like to create a scheme such as I can use it both in the code and in IB, but by defining the colors only once and being able to switch colors so that the colors specified in both the code and IB , changed accordingly.

+8
ios colors xcode palette
source share
1 answer

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

So the code may look 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.. } 

Hope this helps.

Greetings.

Code updated

You can select a set of tags for the controls to which you want to assign a specific color, and then set the color accordingly.

Update 2

According to your edited question, you can have a standalone xib in which you can add different small UIView , and you can define colors from xib . Creating points for different views. And then load the nib into AppDelegate to extract the different colors that you want to use in the application, and you can save them in the Singleton Class so you can get them at any time without multiple initializations.

+2
source share

All Articles