Get global tint color from code
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
Related videos on Youtube
fahu
🇦🇹 Austrian | 💻 Software Engineer at Tractive (previously @Microsoft) | 🥊 Thaiboxer | 🚑 Voluntary Paramedic | 📸 Photographer
Updated on August 19, 2022Comments
-
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 over 10 yearsThere I'm getting the views tintColor but it's not equal to the global tint color.
-
Maximilian Litteral over 10 yearsthe 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 over 10 yearsstrange, 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 over 10 yearsGuess 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é over 10 yearsThis doesn't seem to work.
[[self view] tintColor]
does work however. -
Maximilian Litteral over 10 yearsHow 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é over 10 yearsAh that's the difference, I only set it on the storyboard.
-
William T. almost 8 yearswindow doesn't seem to have a tintColor property anymore.
-
manuelwaldner over 7 yearsusing 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 almost 7 yearsFinally 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
-
lennartk about 4 yearsThis will break on iPads/Catalyst with UIScene, where there would be no keyWindow