5

I have an application with UITabBarController as its main controller.

When user taps a button(not in the tab bar, just some other button), I want to add new UIViewController inside my UITabBarController and show it, but I don't want for new UITabBarItem to appear in tab bar. How to achieve such behaviour?

I've tried to set tabBarController.selectedViewController property to a view controller that is not in tabBarController.viewControllers array, but nothing happens. And if I add view controller to tabBarController.viewControllers array new item automatically appears in the tab bar.

Update

Thanks to Levi, I've extended my tab bar controller to handle controllers that not present in .viewControllers.

@interface MainTabBarController : UITabBarController

/** 
 * By setting this property, tab bar controller will display
 * given controller as it was added to the viewControllers and activated
 * but icon will not appear in the tab bar.
 */
@property (strong, nonatomic) UIViewController *foreignController;

@end


#import "MainTabBarController.h"

@implementation MainTabBarController

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
  self.foreignController = nil;
}

- (void)setForeignController:(UIViewController *)foreignController
{
  if (foreignController) {
    CGFloat reducedHeight = foreignController.view.frame.size.height - self.tabBar.frame.size.height;
    foreignController.view.frame = CGRectMake(0.0f, 0.0f, 320.0f, reducedHeight);

    [self addChildViewController:foreignController];
    [self.view addSubview:foreignController.view];
  } else {
    [_foreignController.view removeFromSuperview];
    [_foreignController removeFromParentViewController];
  }

  _foreignController = foreignController;
}

@end

The code will correctly set "foreign" controller's view size and remove it when user choose item in the tab bar.

lambdas
  • 3,990
  • 2
  • 29
  • 54

2 Answers2

2

You either push it (if you have a navigation controller) or add it's view to your visible View Controller's view and add it as child View Controller also.

Levi
  • 7,313
  • 2
  • 32
  • 44
  • 3
    one thing about adding a child view controller to a UITabBarController. It will actually add the child controller to its viewControllers property. And at times its tab bar will show a ghost item for the child controller. – riadhluke May 22 '15 at 07:33
  • @riadhluke I'm facing similar problem on Bigger devices like iPhone 6 plus, XR etc. Is there any workaround ? – atulkhatri Mar 26 '19 at 08:08
  • @atulkhatri I think you should add the child controller to the visible child view controller of the UITabBarController not to the UITabBarController itself – riadhluke Mar 26 '19 at 08:59
  • Thank you for your suggestion but I tried it and It causes inconsistencies if I add the view in UITabBarController and viewcontroller in the child of UITabBarController. I'm also not able to remove the controller from 'viewcontrollers' array in UITabBarController. – atulkhatri Mar 26 '19 at 09:05
0

You can either present the new view controller with:

- (void)presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion;

Or, if one of your UIViewControllers is inside a UINavigationController, you can:

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated;
Rui Peres
  • 25,741
  • 9
  • 87
  • 137
  • If I present new controller from tab bar controller, tab bar will be hidden. Unfortunately, I can't use second approach too. – lambdas Apr 06 '13 at 13:36
  • There are many screens in the application. Tab bar provides access to the six most frequently used screens. Other screens can be reached through the left sliding menu(like Facebook app). But all the screens should look the same - UINavigationController inside UITabBarController. – lambdas Apr 06 '13 at 14:13