0

When I use the below code from a singleton, UITabBar appearance is not working completely (not changing at all), and UINavigationBar appearance is changing arbitrarily, not according to switch cases.

#pragma mark - UITabBarController Delegate

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    NSLog(@"switch");
    switch ([tabBarController.viewControllers indexOfObject:viewController])
    {
        case 0:
            [[UINavigationBar appearance] setBackgroundImage:[UIColor imageWithColor:[AppConfiguration color3]] forBarMetrics:UIBarMetricsDefault];
            [[UITabBar appearance] setBarTintColor:[AppConfiguration color3]];
            break;
        case 1:
            [[UINavigationBar appearance] setBackgroundImage:[UIColor imageWithColor:[AppConfiguration color4]] forBarMetrics:UIBarMetricsDefault];
            [[UITabBar appearance] setBarTintColor:[AppConfiguration color4]];
            break;
        case 2:
            [[UINavigationBar appearance] setBackgroundImage:[UIColor imageWithColor:[AppConfiguration color5]] forBarMetrics:UIBarMetricsDefault];
            [[UITabBar appearance] setBarTintColor:[AppConfiguration color5]];
            break;

        default:
            NSLog(@"UITabBar index: %lu", (unsigned long)[tabBarController.viewControllers indexOfObject:viewController]);
            break;
    }
}
Bartu
  • 2,189
  • 2
  • 26
  • 50
  • Why not to set all that properties in every view controller? – kirander Oct 07 '15 at 16:43
  • 2 reasons. First, I like to keep things centrally. Second, each tabbar has navigation controllers, which means every view controller will have to implement methods sort of like this. IMO, this should've worked. But it didn't. – Bartu Oct 07 '15 at 16:48
  • As minimum, you should use method appearanceWhenContainedIn:. But all this way is messy. – kirander Oct 07 '15 at 16:53
  • appearanceWhenContainedIn will not work with uitabbar solution. – Bartu Oct 07 '15 at 16:58
  • You are right. Generally, appearance should set once in AppDelegate (as early as possible). In your case, when UITabBarController was instantiated it is too late to change appearance. You can just take the view controller parameter and configure colors directly, like viewController.navigationController.navigationBar.tintColor = – kirander Oct 07 '15 at 17:08

0 Answers0