0

I have a UITabBarController with five child viewcontrollers, namely firstVC, secondVC, etc. The text color for unselected items is set to gray and for the selected item is set to orange. Everything works fine, except when the app navigates to a child view controller from firstVC, secondVC, etc., and then navigates back to the UITabBarController. At this point, all the selected item colors are reset to blue when clicked, which is the default color.

+(void)initialize {
    NSMutableDictionary *dicNormal = [NSMutableDictionary dictionary];
    dicNormal[NSFontAttributeName] = [UIFont systemFontOfSize:12];
    dicNormal[NSForegroundColorAttributeName] =DES_COLOR;
    NSMutableDictionary *dicSelected = [NSMutableDictionary dictionary];
    dicSelected[NSFontAttributeName] = [UIFont systemFontOfSize:12];
    dicSelected[NSForegroundColorAttributeName]=ORANGE_COLOR;
    UITabBarItem *item = [UITabBarItem appearance];
    [item setTitleTextAttributes:dicNormal forState:UIControlStateNormal];
    [item setTitleTextAttributes:dicSelected forState:UIControlStateSelected];
    UITabBar *bar = [UITabBar appearance];
    [bar setBackgroundColor:WHITE_COLOR];
    [bar setBackgroundImage:[[UIImage alloc]init]];
    [bar setShadowImage:[[UIImage alloc]init]];
}

The code for setting the text attributes is in the UITabBarController and is executed in the initialize method. I tried to put the same code in the viewWillAppear method of each child view controller of the UITabBarController. However, if a child view controller has been selected before the push to another view controller, the text color remains blue, whereas for unselected items, the text color changes to orange, which is the desired color.

My question is: How can we ensure that the text color always remains the same color for UIControlStateSelected when navigating back to the UITabBarController from other child viewcontrollers? Thanks in advance.

0 Answers0