抑制自定义UITabBarController中的更多导航控制器

我为一个项目实现了一个自定义的UITabBar解决scheme。 基本上,如果有超过5个项目,我使用scrollView,将允许用户滚动其他选项卡项目,并抑制更多的button。 天气频道的应用程序中可以看到类似的外观和感觉。

每个标签栏项目对应于pipe理每个标签的视图堆栈的UINavigationController。 我遇到的问题是当我有超过5个选项卡项目,从选项卡5起,不正确地维护导航堆栈。 似乎moreNavigationController每次返回到该选项卡时会导致导航堆栈被终止,而您又被带到初始页面。

我已经覆盖了setSelectedViewController方法,如下所示:

- (void) setSelectedViewController:(UIViewController *)selectedViewController { [super setSelectedViewController:selectedViewController]; if ([self.moreNavigationController.viewControllers count] > 1) { self.moreNavigationController.viewControllers = [[NSArray alloc] initWithObjects:self.moreNavigationController.visibleViewController, nil]; } } 

此代码将删除左侧导航button上的更多function,但不能解决维护导航堆栈的问题。 所有其他标签工作正常。 我可以遍历几个视图,并在我离开并返回到该选项卡后维护堆栈。 我明白这是一个复杂的问题,所以请让我知道,如果有一些地方,我可以提供清晰。 谢谢!

这是我最终解决这个问题的方法:

 - (void) setSelectedViewController:(UIViewController *) selectedViewController { self.viewControllers = [NSArray arrayWithObject:selectedViewController]; [super setSelectedViewController:selectedViewController]; } 

基本上从5的任何选项卡获取其导航控制器由moreNavigationControllerreplace时,你在UITabBarController上设置viewControllers。 因此,我dynamic设置viewControllers只包含我点击的选项卡。 在这种情况下永远不会超过1,所以更多的导航控制器不会起作用。

当我启动我的自定义控制器时,我只提供第一个选项卡作为viewControllers,以便应用程序可以加载。

 - (id) init { self = [super init]; if (self) { self.delegate = self; [self populateTabs]; } return self; } - (void) populateTabs { NSArray *viewControllers = [self.manager createViewsForApplication]; self.viewControllers = [NSArray arrayWithObject:[viewControllers objectAtIndex:0]]; self.tabBar.hidden = YES; MyScrollingTabBar *tabBar = [[MyScrollingTabBar alloc] initWithViews:viewControllers]; tabBar.delegate = self; [self.view addSubview:tabBar]; } 

为了清楚起见,tabBar委托被设置为这个类,以便它可以响应标签点击。 委托方法如下:

 - (void) tabBar:(id) bar clickedTab:(MyScrollingTabBarItem *) tab { if (self.selectedViewController == tab.associatedViewController) { [(UINavigationController *) tab.associatedViewController popToRootViewControllerAnimated:YES]; } else { self.selectedViewController = tab.associatedViewController; } // keep nav label consistent for tab self.navigationController.title = tab.label.text; }