Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add/Remove or Show/Hide tab bar items from UITabbarController when using storyboards

I have an application that needs to show different content from a UITabBarController based on if the user is registered or not. Is there a way to add and remove ViewControllers from a UITabBarController at run-time? Show and Hide would be fine too.

Prior to storyboards you could call setViewController but that does not seem to be the right way when using stoaryboards.

like image 384
Dennis Burton Avatar asked Nov 15 '12 17:11

Dennis Burton


2 Answers

You can remove a tabbar item as follows:

NSMutableArray *tabbarViewControllers = [NSMutableArray arrayWithArray: [self.tabBarController viewControllers]];
[tabbarViewControllers removeObjectAtIndex: /*Any index*/];
[self.tabBarController setViewControllers: tabbarViewControllers ];
like image 99
user427969 Avatar answered Nov 05 '22 09:11

user427969


Swift 4+

func removeTab(at index: Int) {
        guard var viewControllers = self.tabBarController?.viewControllers else { return }
        viewControllers.remove(at: index)
        self.tabBarController?.viewControllers = viewControllers
    }
like image 25
Maverick Avatar answered Nov 05 '22 08:11

Maverick