Add controller to UITabBarController without a new item appearing in the tab bar

I have an application with UITabBarController as the main controller.

When the user clicks the button (and not on the tab bar, only on some other button), I want to add a new UIViewController inside my UITabBarController and show it, but I do not want the new UITabBarItem to appear on the tab bar. How to achieve this behavior?

I tried setting the property tabBarController.selectedViewControllerto a view controller that is not in the array tabBarController.viewControllersbut nothing is happening. And if I add a view controller to the array tabBarController.viewControllers, a new item will automatically appear in the tab bar.

Update

Thanks to Levi, I expanded my tab bar controller to handle controllers that are 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 the size of the appearance of the controller and delete it when the user selects an item in the tab bar.

+5
source share
2 answers

You either click it (if you have a navigation controller), or add it to your View Controller and add it as a child view controller.

+2
source

You can introduce a new view controller with:

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

Or, if one of yours UIViewControllersis inside UINavigationController, you can:

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated;
0
source

All Articles