【发布时间】:2013-03-28 21:33:45
【问题描述】:
我有一个以 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
当用户在标签栏中选择项目时,代码将正确设置“外国”控制器的视图大小并将其删除。
【问题讨论】:
-
你为什么不直接展示新的视图控制器呢?
-
标签栏应该是可见的..
-
Hmmmm...然后你要么推它(如果你有一个导航控制器)或将它的视图添加到你可见的视图控制器的视图中,并将其添加为子视图控制器
-
后一种方法对我有用。谢谢你。您可以将其添加为答案。
标签: ios cocoa-touch uitabbarcontroller uitabbaritem