1

When I add a view controller embedded by navigation controller, to a tab bar, its icon + title disappear briefly when coming back to the More tab.

However when the view controller is added as such, the icon+image are okay and don't disappear.

I've tried many things already, and am out of options. Any ideas?

Here's my AppDelegate code:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.tabBarController = [[UITabBarController alloc] init];

    // Must be placed here, just before tabs are added.  Otherwise navigation bar
    // will overlap with status bar.
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationSlide];

    [self addViewControllersToTabBar];
    self.window.rootViewController = self.tabBarController;
    self.window.backgroundColor    = [UIColor whiteColor];
    [self.window makeKeyAndVisible];

    return YES;
}

- (void)addViewControllersToTabBar
{
    NSArray* tabBarClassNames =
    @[
      NSStringFromClass([FirstViewController  class]),
      NSStringFromClass([SecondViewController class]),
      NSStringFromClass([FirstViewController  class]),
      NSStringFromClass([FirstViewController  class]),
      NSStringFromClass([FirstViewController  class]),
      NSStringFromClass([SecondViewController class]),
      NSStringFromClass([FirstViewController  class]),
      ];

    NSMutableArray* viewControllers = [NSMutableArray array];
    for (NSString* className in tabBarClassNames)
    {
        UIViewController*       viewController = [[NSClassFromString(className) alloc] init];
        UINavigationController* navigationController;

        navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];

        [viewControllers addObject:navigationController];
    }

    [viewControllers addObject:[[FirstViewController alloc] init]]; // This one is fine.

    self.tabBarController.viewControllers        = viewControllers;
    self.tabBarController.selectedViewController = viewControllers[2];
}

and the view controllers are literally nothing more than:

@implementation SecondViewController

- (instancetype)init
{
    if (self = [super init])
    {
        self.title            = @"second";
        self.tabBarItem.image = [UIImage imageNamed:@"second.png"];
    }

    return self;
}

@end

enter image description here

meaning-matters
  • 21,929
  • 10
  • 82
  • 142

1 Answers1

0

Holy shit, spent so much time on this bug, but here's my workaround. Instead of:

navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];

I created my own NavigationController as subclass of UINavigationController:

@implementation NavigationController

- (instancetype)initWithRootViewController:(UIViewController*)rootViewController
{
    if (self = [super initWithRootViewController:rootViewController])
    {
        NSString* className = NSStringFromClass([rootViewController class]);
        NSString* name      = [className stringByReplacingOccurrencesOfString:@"ViewController" withString:@""];

        self.tabBarItem.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@Tab.png", name]];
    }

    return self;
}

@end

and then do:

navigationController = [[NavigationController alloc] initWithRootViewController:viewController];

Prerequisite is that tab images have the same base name as the view controller class name, which was already the case in my app.

I was setting self.tabBarItem.image inside the view controllers' init method, and this seems to cause the effect I was seeing. So in addition to using my own navigation controller, I also simply deleted setting the tabBarItem in each individual view controller.

meaning-matters
  • 21,929
  • 10
  • 82
  • 142
  • I just came upon this same issue. Instead of subclassing though, I found I could just set the tabBarItem after initialization of the UINavigationController. It may just be my particular case, but just throwing it out there if anyone else gets this issue they have another option to try as well. – kailoon Mar 14 '16 at 16:51
  • @kailoon Was that on iOS 7, like my issue? – meaning-matters Mar 15 '16 at 10:31