Get global tint color from code

13,822

Solution 1

In the app delegate you can set it by

UIColor *globalTint = [[[UIApplication sharedApplication] delegate] window].tintColor;

Solution 2

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.

Solution 3

[UIApplication sharedApplication].delegate.window.rootViewController.view.tintColor

Seems to work.

Solution 4

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.

Solution 5

Swift 4.x:

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

Usage:

textField.textColor = .tintColor
Share:
13,822

Related videos on Youtube

fahu
Author by

fahu

🇦🇹 Austrian | 💻 Software Engineer at Tractive (previously @Microsoft) | 🥊 Thaiboxer | 🚑 Voluntary Paramedic | 📸 Photographer

Updated on August 19, 2022

Comments

  • fahu
    fahu over 1 year

    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.

  • fahu
    fahu over 10 years
    There I'm getting the views tintColor but it's not equal to the global tint color.
  • Maximilian Litteral
    Maximilian Litteral over 10 years
    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.
  • fahu
    fahu over 10 years
    strange, because I get blue color with you solution, although I adjusted orange color in the File Inspector and the buttons color is orange.
  • Maximilian Litteral
    Maximilian Litteral over 10 years
    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.
  • René
    René over 10 years
    This doesn't seem to work. [[self view] tintColor] does work however.
  • Maximilian Litteral
    Maximilian Litteral over 10 years
    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
  • René
    René over 10 years
    Ah that's the difference, I only set it on the storyboard.
  • William T.
    William T. almost 8 years
    window doesn't seem to have a tintColor property anymore.
  • manuelwaldner
    manuelwaldner over 7 years
    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.
  • Efren
    Efren almost 7 years
    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.vi‌​ew.tintColor
  • lennartk
    lennartk about 4 years
    This will break on iPads/Catalyst with UIScene, where there would be no keyWindow