I am using UITabBarController and added tabs using relationship segue from storyboard.
How to hide particular tab according to logged in user role?
I am using UITabBarController and added tabs using relationship segue from storyboard.
How to hide particular tab according to logged in user role?
On
Nice question!
You need to dig UITabbarController and its members (properties + functions)
Now, focus on these steps to find solution:
viewControllers (which an array of UIViewController) that stores UIViewControllers for your Tabbar Controller associated using UITabbar items.viewControllers property of tabbar controller.Here is sample example on application launch:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
if var tabController = self.window?.rootViewController as? UITabbarController, var viewControllers = tabController.viewControllers {
let isLoggedIn = <get value from your data storage as bool>
if isLoggedIn {
viewControllers.remove(at: firstIndex) // By considering you need to remove view controller at index first. It will automatically remove tab from tabbar also.
tabController.viewControllers = viewControllers
self.window?.rootViewController = tabController
// further operations to make your root controller visible....
}
}
}
If you want to remove tabs from your tab bar controller do something like this (When your user is not logged in)
when your user logs in
Swift Version
Remove tab
Add tab