Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variables for colors in interface builder

I have a project that I need to change overall color themes in the app. A lot of my UI elements are built through Interface Builder in Xcode 6.1. I need to set colors as variables in interface builder, so if I set a preprocessor telling the app to use a certain scheme then the colors will change in interface builder. Is this even possible?

like image 945
jamespick Avatar asked Nov 05 '14 22:11

jamespick


People also ask

How do I select colors in Xcode?

To use the color picker in Xcode, you can access it in two ways: Go to the Xcode's toolbar and click Edit -> Format -> Show Colors.

What happens when color or Uicolor has values outside 0 1?

Suggested approach: In the old days values outside of 0 and 1 would be clamped (i.e., forced to 0 or 1) because they didn't mean anything, but wide color support means that is no longer the case – a red value beyond 1.0 is especially red, going into the Display P3 gamut.


1 Answers

I'm not aware of any way to do this with interface builder, however there is a way that you can set appearance properties in code for many IOS UI elements that will then apply globally. As an example see the following snippet of code:

UIToolbar.appearance().tintColor = UIColor.whiteColor()
UIToolbar.appearance().barTintColor = UIColor.blackColor()

UITableView.appearance().separatorColor = UIColor.grayColor()
UITableView.appearance().sectionIndexColor = UIColor.grayColor();

UINavigationBar.appearance().tintColor = UIColor.blueColor()
UINavigationBar.appearance().barTintColor = UIColor.blackColor()
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName : UIColor.whiteColor()]

The code sets a the default tint and bar color for all the UIToolbars, separator and section index colors for all UITableViews, and appearance properties for all the UINavigation views in my app..

You could use #if to set the appearance differently depending on the environment variables set in the compiler.

If you want to find out more about how this works Id suggest reading Apples documentation on UIAppearance properties here: UIAppearance Documentation

like image 172
Ron Avatar answered Sep 18 '22 08:09

Ron