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
@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.
source
share