我有一个以 UITabBarController 作为其主控制器的应用程序。
当用户点击一个按钮(不在标签栏中,只是其他按钮)时,我想在我的 UITabBarController 中添加新的 UIViewController 并显示它,但我不希望新的 UITabBarItem 出现在标签栏中。如何实现这种行为?
我试图将tabBarController.selectedViewController
属性设置为不在tabBarController.viewControllers
数组中的视图控制器,但没有任何反应。如果我将视图控制器添加到tabBarController.viewControllers
数组中,新项目会自动出现在选项卡栏中。
更新
感谢 Levi,我扩展了我的标签栏控制器来处理不存在于.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
当用户在标签栏中选择项目时,代码将正确设置“外部”控制器的视图大小并将其删除。