cgreendyk104
cgreendyk104

Reputation: 59

Swift Color Assets between different targets

I have an app with a couple of different targets. I have create two different asset folders with a set of colors. Both assetfolders have the same names for each color, however different colors.

I am calling the color codes in the code like this: let color = UIColor(named: "primary"). However when I am running the app on a different target, it pulls the primary color from the other target. Both files are only checked to the appropriate target, and unchecked for all other targets.

I am trying to use the same name, with different colors between targets. Does anybody have any tips for me? :)

enter image description here

Upvotes: 4

Views: 2777

Answers (3)

Wladislaw
Wladislaw

Reputation: 1250

Both provided answers are fine. But speaking here from year 2021 the best approach would probably be to combine both methods. Use protocol that will resolve your colors from your main bundle and use inside the protocol a class, that resolves your colors from color set.

The possibility that you name your colors wrong in this one class is very low.

The advantage of mixing this things that way up is, that you use dynamic colors for high contrast (accessibility), dark mode and normal colors in one place. Your colors of the app are changing at the moment user change the setting. And you can still test it, because you are using protocol to wrap this colors.

Upvotes: 0

Raul Mantilla
Raul Mantilla

Reputation: 344

Don't use assets, use a protocol and multiple files for each theme, using asset can cause you future crashes, due you will have to know the exact name of the Color set, and use color names as Strings among the entire app.

Using protocols for each theme will assure you all the themes have the colors and font you need.

Here. I created a project to demonstrate how to implement multiThemes

https://github.com/msistemas26/multipleThemesSwift

Upvotes: 0

jlew
jlew

Reputation: 10601

Guessing that UIColor(named: "primary", in: bundleFromTheRightTarget, compatibleWith: nil) would be enough to disambiguate which place it should load from

Upvotes: 3

Related Questions