Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS - Storyboard - Use a programmatically define color in the configuration palette of storyboard

I try to find a way to deal with multiple targets (deploy multiple app from one project), i don't know how to define a UIColor and use it in storyboard (i can do that well for UIImage). My idea is to switch the value of this color according of the target with macros, someone know if it's possible ?

Perhaps define a color :

var myColor : UIColor
#ifdef target1
myColor = UIColor(red: 249.0/255, green: 249.0/255, blue: 249.0/255, alpha: 1.0)
#ifdef target2
myColor = UIColor(red: 210/255, green: 100/255, blue: 90/255, alpha: 1.0)
#endif

Or duplicate storyboard to have one storyboard by target. Use the info-target1.plist, info-target2.plist ?

And call this variable "myColor" in storyboard

If there is no solution, i think that i need to set all the attributes : color, font, size, programmatically and not in storyboard.

Which method do i need to follow?

Thanks

like image 666
Jaysee Avatar asked Dec 20 '14 08:12

Jaysee


2 Answers

As of Xcode 9 you can define and name color inside an Asset; just open the asset in Xcode, right-click and select "New Color Set". Once defined, colors can be selected in the storyboard color picker by name or referenced programmatically, for example:

let green = UIColor(named: "MySpecialGreen")
like image 165
sherb Avatar answered Oct 25 '22 11:10

sherb


If there is no solution, i think that i need to set all the attributes : color, font, size, programmatically and not in storyboard.

There's no way to vary colors or other attributes in a storyboard scene based on compile-time parameters. Usually, the right way to approach something like this is to use UIAppearance to override the attributes that you want to change. In a nutshell, any class that implements the UIAppearance protocol can provide a proxy object that lets you adjust the traits that class will use. For example, if you want to adjust the navigation bar's color in your app, define myColor conditionally as you've done and then use UINavigationBar's appearance proxy to set the tint color:

[[UINavigationBar appearance] setBarTintColor:myColor];
like image 40
Caleb Avatar answered Oct 25 '22 12:10

Caleb