2

I'm doing the following code to make a system-wide theme change to my app for button colors:

UIWindow *window = [[[UIApplication sharedApplication] windows] lastObject];
window.tintColor = [UIColor themeColorNamed:@"barAndButtonTextColor"];

The question is, how do I turn it back to the default color? I have tried this with no luck:

window.tintColor = nil;

When I try to get the default UIColor when the app is initially loading and store it, it's coming back as nil in my AppDelegate:

UIColor *myColor = window.tintColor;

Any ideas on how I can solve this? I don't want to hardcode the color because who knows what could change in future iOS versions.

Ethan Allen
  • 14,425
  • 24
  • 101
  • 194

2 Answers2

3

Setting it to nil is the correct way to restore the default value.

I'm guessing that lastWindow is no longer the same window when you're trying to restore the default tint color. The system may have added windows for other reasons.

Instead of using lastObject in the windows array, I'd use keyWindow for setting it both times:

UIWindow *window = [UIApplication sharedApplication].keyWindow;
Dave Batton
  • 8,795
  • 1
  • 46
  • 50
  • It would be interesting to check how many windows there are (i.e., `[[[UIApplication sharedApplication] windows] count]`), and which one holds his app's root view controller. I know system alerts are presented on a separate window on top of the app's one, and also you can have more than one window when supporting external monitors, but never experimented on that... – Nicolas Miari Mar 10 '16 at 03:59
3

You might try

window.tintColor = [[UIView new] tintColor];

I'm doing this within application:didFinishLaunchingWithOptions: when I initialize my window, because for whatever reason, the window's tint color is defaulting to nil.

Somehow despite being nil, I never encountered a problem, but I've had users of my app send me screenshots of Alert Controllers and Action Sheets where the button text is invisible, and I'm quite sure it must be related to the window's tint color having been nil.

beebcon
  • 6,893
  • 5
  • 25
  • 27