6

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

jimbob
  • 3,288
  • 11
  • 45
  • 70

2 Answers2

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.

Léo Natan
  • 56,823
  • 9
  • 150
  • 195
  • 1
    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? – jimbob Oct 28 '13 at 17:46
  • You say you need to change the tint in runtime, in which case this is a better solution. – Léo Natan Oct 28 '13 at 18:16
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 *)]
Gonzo
  • 1,533
  • 16
  • 28
  • 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? – jimbob Oct 28 '13 at 17:44
  • UIAppearance is recommended, it will change the tint color of all subclasses too. Like buttons, toolbars... – Gonzo Oct 28 '13 at 17:49
  • @CEAFDC: Any `UIView` descendant will inherit the superview's `tintColor` if it isn't explicitly set. – Scott Berrevoets Oct 28 '13 at 17:53
  • Yeah, I know. Booth methos will have the same result. But this applies just to tint color, UIAppearance is more general – Gonzo Oct 28 '13 at 17:58
  • 2
    This method will not change existing controls' tint color after they have been created. If @jimbob needs to change the tint color in runtime, setting the window's tint color is a better option. – Léo Natan Oct 28 '13 at 18:23
  • 4
    `UIView`'s `tintColor` does not have `UI_APPEARANCE_SELECTOR`. See `UIView.h`. – JRG-Developer Oct 28 '13 at 18:28
  • 3
    Ok, now I'm convinced. Thanks :) UIWindow tintColor is the way to go – Gonzo Oct 28 '13 at 18:52
  • There's also an overrideable tintColorDidChange on UIView, described here: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/#//apple_ref/occ/instm/UIView/tintColorDidChange – atlex2 Dec 22 '15 at 15:01