Search code examples
iostabbarcontroller

Remove all objects in tabBarController and later restore the default tabBar?


I am looking for the behaviour in iOS to remove all the items/objects in my tabBar but also be able to restore them later on. To remove all items/objects the following code is working fine:

NSMutableArray *controllers = [NSMutableArray arrayWithArray:self.tabBarController.viewControllers];
[controllers removeAllObjects];
[self.tabBarController setViewControllers:controllers animated:YES];

But how do I restore the default tabBar once again after it being removed? Try to be specific.

Thanks in advance.


Solution

  • // backup the current viewcontrollers
    NSArray *viewControllerBackup = self.tabBarController.viewControllers;
    
    // remove tabs
    [self.tabBarController setViewControllers:@[] animated:YES];
    
    // restore tabs
    [self.tabBarController setViewControllers:viewControllerBackup animated:YES];