0
votes

I am writing an app with multiple targets and want to change the default tint color for each target individually. I have a file containing constants for each target, including the definition for the tint color, kTintColor.

This is what I tried to do:

self.window?.tintColor = kTintColor
UIView.appearance().tintColor = kTintColor

The tint color changes as i want to, but it causes another problem:
In some of my views, I need to change the tint of a single UIBarButtonItem while the others keep the global tint. This is achieved by calling self.barBtnX.tintColor = ...
If I user the global tint color set in my storyboard file, this works as expected. However, after changing the tint color in my AppDelegate it stops working.

Is there another way to do this?

EDIT:
I am able to change the global tint color for each target, but after I do so, I can no longer change the tint color of a single UIBarButtonItem. I want to know if there is a way to achieve both.

2

2 Answers

0
votes

First, you can make a dictionary with each key/value pair being a target name/tint color value.

var TintColorDictionary:NSDictionary = [
    "target_name_1": "tint_color_1",
    "target_name_2": "tint_color_2",
              "...": "..."
];

Then, take a look here: How to get a target name in iOS.

Then just pass in the target name and get your tint color!

Good luck!

0
votes

I found a solution to my problem. Here is a step by step guide:

TLDR:
Don't change the tint color, change the image rendering mode.

Define tint colors
Add file called Constants-TargetName.swift for each target and add it to the buid settings of the corresponding target. Inside that file goes the definition for the global tint color (and other global constants for that target if needed).
let kTintColor = UIColor(red: 0.0, green: 0.0, blue: 170.0/255.0, alpha: 1.0)

Set global tint color
Change the global tint color in AppDelegate.swift, inside application(didFinishLaunchingWithOptions):
UIView.appearance().tintColor = kTintColor

Change the color of a UIBarButtonItem
Set your image to have the color you want to change it to. I my case, I have a blue global tint color and the images are gray. So the code below switches colors between gray and blue. Instead of changing the tint color for a single UIBarButtonItem, change its image:

if globalTintColor {
  self.btn.image = UIImage(named: "ImgName")?.imageWithRenderingMode(.AlwaysTemplate)
} else {
  self.btn.image = UIImage(named: "ImgName")?.imageWithRenderingMode(.AlwaysOriginal)
}