-1

I have a tab bar controller with four tabs. For one tab, i have created a new storyboard with navigation view controller and simple UIViewController. For other tabs, connected view controllers with navigation controller within the same storyboard.

When i try to launch first view controller from new storyboard from first tab, it is not showing tab bar. for others it is showing tab bar properly.

 -(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
    NSInteger index = [tabBar.items indexOfObject:item];
    if(index == 0){
        UIViewController *suggestionsViewCintroller = (UIViewController *)[[UIStoryboard storyboardWithName:@"suggestions_view" bundle:nil] instantiateViewControllerWithIdentifier:@"suggestions_view_controller"];
        [self addChildViewController:suggestionsViewCintroller];
        [self.view addSubview:suggestionsViewCintroller.view];

        suggestionsViewCintroller.hidesBottomBarWhenPushed = NO;
        [suggestionsViewCintroller didMoveToParentViewController:self];
    }
}

Navigation controller configuration in storyboard : enter image description here

View controller and tab bar controller are in different storyboards.

Why is it not showing tab bar in view?

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Rajeev
  • 4,762
  • 8
  • 41
  • 63
  • Have you tried using storyboard references? You shouldn't really be manually adding child view controllers for tab bar items. – Matthew Hallatt Apr 03 '18 at 14:10
  • view controller and tab bar controller are in different story boards – Rajeev Apr 03 '18 at 14:12
  • Yes, but using Storyboard References, you should be able to set a second storyboard as the view controller for a tab in a tab bar controller. – Matthew Hallatt Apr 03 '18 at 14:13
  • can u please share your solution. I am not aware of that – Rajeev Apr 03 '18 at 14:14
  • 3
    This article goes through in more depth: https://code.tutsplus.com/tutorials/ios-9-staying-organized-with-storyboard-references--cms-24226 But briefly, in the storyboard where your tab bar view controller is defined, drag a new "Storyboard Reference" item from the Object Library and set this as a view controller in your tab bar controller. – Matthew Hallatt Apr 03 '18 at 14:19
  • @MatthewHallatt very valuable response. Thanx – Rajeev Apr 03 '18 at 14:28

2 Answers2

1

I don't think that is the proper way to add a view controller to the tab bar. I think you want to sub class your UITabBarController and add the new view controller to the UITabBarController sub class in the viewDidLoad method of the sub class. You can see an example of this here. The main code is added in TabBarController (a sub class of UITabBarController)

- (void)viewDidLoad {
    [super viewDidLoad];

    UINavigationController* naviController = [[UIStoryboard storyboardWithName:@"Other" bundle:nil]
                                           instantiateViewControllerWithIdentifier:@"NavigationController"];
    naviController.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Suggestions"
                                                          image:[UIImage imageNamed:@"suggestions"] tag:2];
    self.viewControllers = [self.viewControllers arrayByAddingObject:naviController];
}

Edited the example to use 2 different storyboards.

Main storyboard:

Main storyboard

Other storyboard:

enter image description here

Video here.

Popmedic
  • 1,791
  • 1
  • 16
  • 21
0

Calling 'addChildViewController' you do not add tab in UITabBarController. This call just add child view controller over whole view of UITabBarController. So it is quit reasonable, that you can not see tabbar.

If you wish to add view controller as TAB of UITabBarController, you should use 'viewControllers' property as shown here: https://stackoverflow.com/a/11399634/4322841

And should maybe use 'selectedIndex' property.

Artem
  • 331
  • 1
  • 13