How can I change the global tint color programmatically? [duplicate]
Asked Answered
M

2

6

My project needs to switch between 2 different global tint values. How can I do this programmatically?

Microgamete answered 28/10, 2013 at 17:40 Comment(0)
U
8

Change the tint color of the UIWindow of the application. You can either use the [[UIApplication sharedApplication] keyWindow] but better is to use [[UIApplication sharedApplication] delegate].window.

Urinary answered 28/10, 2013 at 17:42 Comment(2)
this is the code I have wrote: UIWindow* mWindow = [[UIApplication sharedApplication] keyWindow]; mWindow.tintColor = [UIColor themeColorNamed:@"themeColour"]; CEAFDC also suggested a good answer. Which is most correct?Microgamete
You say you need to change the tint in runtime, in which case this is a better solution.Latoyialatreece
E
8

UIAppearance is the answer! It sets property to all the objects of that class (and subclasses).

[[UIView appearance] setTintColor:(UIColor *)]

You can change the backgroudColor of all the buttons too

[[UIButton appearance] setBackgroundColor:(UIColor *)]
Emulsify answered 28/10, 2013 at 17:44 Comment(8)
hrmm I was about to do this: UIWindow* mWindow = [[UIApplication sharedApplication] keyWindow]; mWindow.tintColor = [UIColor themeColorNamed:@"themeColour"]; Is this incorrect? Should I just do UIView appearance instead? It is the global value I need to change... but I guess everything is a view?Microgamete
UIAppearance is recommended, it will change the tint color of all subclasses too. Like buttons, toolbars...Emulsify
@CEAFDC: Any UIView descendant will inherit the superview's tintColor if it isn't explicitly set.Ninette
Yeah, I know. Booth methos will have the same result. But this applies just to tint color, UIAppearance is more generalEmulsify
This method will not change existing controls' tint color after they have been created. If @Microgamete needs to change the tint color in runtime, setting the window's tint color is a better option.Latoyialatreece
UIView's tintColor does not have UI_APPEARANCE_SELECTOR. See UIView.h.Fog
Ok, now I'm convinced. Thanks :) UIWindow tintColor is the way to goEmulsify
There's also an overrideable tintColorDidChange on UIView, described here: developer.apple.com/library/ios/documentation/UIKit/Reference/…Trismus

© 2022 - 2024 — McMap. All rights reserved.