0

I have a UITabBarController for which I have 3 view controllers for 3 tab bar items connected using relationship segues.

But I want to show 2 OR 3 tab bar items depending upon user type. For instance, for user A show only 2 UITabBarItem and for another user B show all 3 UITabBarItem.

How can I achieve this?

Nauman Zafar
  • 1,083
  • 15
  • 40

2 Answers2

2

Try the following approach in your custom tab bar implementation (you will have to set it as a class of the tab bar controller in your storyboard):

class CustomTabBarController: UITabBarController {

    override func awakeFromNib() {
        super.awakeFromNib()

        // I have no idea how you determine your user type, so fix it according to yourself
        if user.type = "A" {
            self.viewControllers = [storyboard!.instantiateViewController(withIdentifier: "ViewController1"),
                                    storyboard!.instantiateViewController(withIdentifier: "ViewController2")]
        } else {
            self.viewControllers = [storyboard!.instantiateViewController(withIdentifier: "ViewController1"),
                                    storyboard!.instantiateViewController(withIdentifier: "ViewController2"),
                                    storyboard!.instantiateViewController(withIdentifier: "ViewController3")]
        }
    }
}
Milan Nosáľ
  • 19,169
  • 4
  • 55
  • 90
0

Possible solutions :-

  1. Add UITabBarItems programatically
  2. Remove the one UITabBarItem programatically when want only 2 UITabBarItems
  3. Add the third UITabBarItem when required

Programatically Adding UITabBarItems :-

UITabBarItem * itemNew = [[UITabBarItem alloc] initWithTitle:@"Page 1"
                                                     image:[UIImage imageNamed:@"page1_image_normal"]
                                             selectedImage:[UIImage imageNamed:@"page1_image_selected"]];

Get existing tabBarItems

NSMutableArray *tbItems = [NSMutableArray arrayWithArray:[self.tabBar items]];
//Add your new tabBarItem
[tbItems addObject:itemNew];

//Set your tabBar items to the new array
[self.tabBar setItems:tbItems];