xcode 使用故事板时从 UITabbarController 添加/删除或显示/隐藏标签栏项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13403247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add/Remove or Show/Hide tab bar items from UITabbarController when using storyboards
提问by Dennis Burton
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.
我有一个应用程序需要UITabBarController
根据用户是否注册来显示不同的内容。有没有办法在运行时ViewController
从 a添加和删除s UITabBarController
?显示和隐藏也可以。
Prior to storyboards you could call setViewController
but that does not seem to be the right way when using stoaryboards.
在使用故事板之前,您可以调用,setViewController
但在使用故事板时这似乎不是正确的方法。
回答by user427969
You can remove a tabbar item as follows:
您可以按如下方式删除标签栏项目:
NSMutableArray *tabbarViewControllers = [NSMutableArray arrayWithArray: [self.tabBarController viewControllers]];
[tabbarViewControllers removeObjectAtIndex: /*Any index*/];
[self.tabBarController setViewControllers: tabbarViewControllers ];
回答by Maverick
Swift 4+
斯威夫特 4+
func removeTab(at index: Int) {
guard var viewControllers = self.tabBarController?.viewControllers else { return }
viewControllers.remove(at: index)
self.tabBarController?.viewControllers = viewControllers
}