简体   繁体   中英

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 ViewController s 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.

You can remove a tabbar item as follows:

NSMutableArray *tabbarViewControllers = [NSMutableArray arrayWithArray: [self.tabBarController viewControllers]];
[tabbarViewControllers removeObjectAtIndex: /*Any index*/];
[self.tabBarController setViewControllers: tabbarViewControllers ];

Swift 4+

func removeTab(at index: Int) {
        guard var viewControllers = self.tabBarController?.viewControllers else { return }
        viewControllers.remove(at: index)
        self.tabBarController?.viewControllers = viewControllers
    }

While designing in storyboard we can hide the tab bar using the option Bottom-bar as none in file-inspector.

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM