9

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

Jaysee
  • 111
  • 1
  • 4

2 Answers2

4

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")
sherb
  • 5,845
  • 5
  • 35
  • 45
  • This is very helpful info, saved my time. Is there any other way for font as well? – Krishna Kirana Mar 26 '19 at 09:30
  • @KrishnaKirana I haven't run into this yet (I'm just starting out) but Apple has a nice article on using custom fonts: https://developer.apple.com/documentation/uikit/text_display_and_fonts/adding_a_custom_font_to_your_app – sherb Mar 26 '19 at 18:31
2

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];
Caleb
  • 124,013
  • 19
  • 183
  • 272