My project needs to switch between 2 different global tint values. How can I do this programmatically?
How can I change the global tint color programmatically? [duplicate]
Asked Answered
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
.
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
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 *)]
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 general –
Emulsify
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 go –
Emulsify
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.