How to hide tabs in tabBarController dynamically in swift 3

5.1k views Asked by At

I am using UITabBarController and added tabs using relationship segue from storyboard.

How to hide particular tab according to logged in user role?

2

There are 2 answers

5
Krunal On

Nice question!

You need to dig UITabbarController and its members (properties + functions)

Now, focus on these steps to find solution:

  1. You can create as many tabbar (item) as you want, using View controllers assocated with it by Segue. Connect all view controllers with tabbar (controller) using segue in your story board.
  2. Now, about UITabbarController: It has a property (variable) viewControllers (which an array of UIViewController) that stores UIViewControllers for your Tabbar Controller associated using UITabbar items.
  3. You need to store a value, that indicates user login status, in permanent storage (like UserDefaults, CoreData,....) of device.
  4. When your application launches, retrieve/get that value (user login status) from storage and use it for further operations on UITabbarController.
  5. Now focus on step 2: Get all view controllers programatically from your tabbar controller using property 'viewControllers` and store in separate variable.
  6. Interchange/Exchange/Update position (index) of ViewControllers in array (You can also remove or insert view controller programatically) according to your requirement (login status) and reassign the same array (variable) to 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....
       }

   }

}
5
Pratik Jamariya On

If you want to remove tabs from your tab bar controller do something like this (When your user is not logged in)

NSInteger indexToRemove = 0;
NSMutableArray *tabs = [NSMutableArray arrayWithArray:self.tabBarController.viewControllers];
[tabs removeObjectAtIndex:indexToRemove];
self.tabBarController.viewControllers = tabs;

when your user logs in

UIViewController *viewController = [[UIViewController alloc] init];
NSMutableArray *tabs = [NSMutableArray arrayWithArray:self.tabBarController.viewControllers];
[tabs addObject:viewController];
self.tabBarController.viewControllers = tabs;

Swift Version

Remove tab

let indexToRemove = 0
if var tabs = self.tabBarController?.viewControllers {
  tabs.remove(at: indexToRemove)
  self.tabBarController?.viewControllers = tabs
} else {
  print("There is something wrong with tabbar controller")
}

Add tab

let indexToAdd = 2
let vc = UIViewController.init()
if var tabs = self.tabBarController?.viewControllers {
  tabs.append(vc) // Append at last index of array
  // tabs.insert(vc, at: indexToAdd)  // Insert at specific index
  self.tabBarController?.viewControllers = tabs
} else {
  print("There is something wrong with tabbar controller")
}