25

Is there a way I can get the global tint color from my project by code? To avoid a misunderstanding I mean the global tint color, which i can set in the File Inspector.

Jano
  • 62,815
  • 21
  • 164
  • 192
fahu
  • 1,511
  • 4
  • 17
  • 26

6 Answers6

33

Easy.

Objective C:

UIColor *tintColor = [[self view]tintColor];

Swift:

let tintColor = self.view.tintColor;

This should get the tintColor set on the app. If you change it, this property should get updated. This assumes you're inside a viewController or a subclass of one and that you haven't overridden the tintColor in some superView between this view and the window.

Update: Notice if you are attempting to get the tint color of a view controller that has not been added to the window then it will not have the custom tint color since this color is inherited from the window object. Thanx to @ManuelWa for pointing this out in the comments.

SmileBot
  • 19,393
  • 7
  • 65
  • 62
  • 1
    using this when the viewController is not yet on e.g. the navigation stack of the rootViewController of your window. It will return the standard tintColor of the system, which can differ from your custom one, if you set one by yourself. – manuelwaldner Jan 16 '17 at 10:20
32

In the app delegate you can set it by

UIColor *globalTint = [[[UIApplication sharedApplication] delegate] window].tintColor;
Maximilian Litteral
  • 3,059
  • 2
  • 31
  • 41
  • 1
    There I'm getting the views tintColor but it's not equal to the global tint color. – fahu Oct 02 '13 at 13:27
  • the global tint color should be the window's tint color. This is getting the window. It works for me, or you can get the app delegate, get the window from there and the tint color again. There is no other way of getting the tint color. – Maximilian Litteral Oct 02 '13 at 13:39
  • 1
    strange, because I get blue color with you solution, although I adjusted orange color in the File Inspector and the buttons color is orange. – fahu Oct 02 '13 at 14:01
  • 2
    Guess you can't get it then. I just set the windows tint in the app delegate and it works as a global tint. But as far as I know there's no API for global tint besides that. – Maximilian Litteral Oct 02 '13 at 15:07
  • This doesn't seem to work. `[[self view] tintColor]` does work however. – René Jan 06 '14 at 14:17
  • How are you setting the tint color though? I get the window and set the tint for the whole app so this should get it. Ill edit my answer with a better way but is basically the same – Maximilian Litteral Jan 06 '14 at 22:36
  • Ah that's the difference, I only set it on the storyboard. – René Jan 09 '14 at 19:23
  • window doesn't seem to have a tintColor property anymore. – William T. Jun 04 '16 at 23:02
2

Max's answer is correct, but I found out that you have to get the navigationController's window:

self.navigationController.view.window.tintColor = [UIColor redColor];

However, note that this wouldn't work if you have set the tintColor manually from Storyboard. The value from Storyboard will be used if you have done so. I've filed a bug with Apple on this. I think this code shouldn't be ignored even if we've set the tintColor from Storyboard.

Enrico Susatyo
  • 19,372
  • 18
  • 95
  • 156
2
[UIApplication sharedApplication].delegate.window.rootViewController.view.tintColor

Seems to work.

nurider
  • 1,555
  • 1
  • 18
  • 21
  • Finally an updated answer! thank you! Tested on App delegate: `self.window?.rootViewController?.view.tintColor`. And in a UITabBarController's view: `UIApplication.shared.delegate?.window?.rootViewController.view.tintColor` – Efren May 10 '17 at 07:39
1
[UIApplication sharedApplication].keyWindow.tintColor;
FunkyKat
  • 3,233
  • 1
  • 23
  • 20
1

Swift 4.x:

extension UIColor {
    static var tintColor: UIColor {
        get {
            return UIApplication.shared.keyWindow?.rootViewController?.view.tintColor ?? .red
        }
    }
}

Usage:

textField.textColor = .tintColor
Ramis
  • 13,985
  • 7
  • 81
  • 100