Remove / Add (Hide) UITabbarItems on the fly

Some days ago, I searched for a solution to hide a TabbarItem from my app but select it programmatically. In all forums and blogs was written, that this is not possible. Hiding a TabbarItem means to remove it from the Tabbar or, on other places, hiding means to disable it, but a disabled Item is still visible only not selectable.

What to do now? Is there an intermediate solution? Yes, it is, somehow:

Firstly we create a normal Tabbar based application with all, for example, 5 UIViewControllers inside of the UITabBarController. Then in the didFinshedLaunchingWithOptions method in the AppDelegate, we remove the one ViewController from the TabBar again, which we do not want to show.

NSMutableArray *viewControllers = (NSMutableArray *) self.tabBarController.viewControllers;
[viewControllers removeObjectAtIndex:4];
[self.tabBarController setViewControllers:viewControllers];

In the ViewController, where we want to access this „hidden“ controller, we can now call in the button selector:

UIViewController *fourthViewController = [[UIViewController alloc] init];
NSMutableArray *viewControllers = (NSMutableArray *) self.tabBarController.viewControllers;
[viewControllers  insertObject:fourthViewController atIndex:4];
[self.tabBarController setViewControllers: viewControllers];

On leaving this View, we can call again the code from AppDelegate and the TabBarItem is removed again.

For sure, not the nicest code but it works.

This logic can also be used, if you want to hide / show single TabBarItems on special tab views.
Print Friendly, PDF & Email

Leave a Comment

Deine E-Mail-Adresse wird nicht veröffentlicht.