我为项目实现了自定义UITabBar解决方案.基本上,如果有超过5个项目,我使用scrollView,允许用户滚动其他选项卡项并禁止更多按钮.在Weather Channel应用程序中可以看到类似的外观.
每个标签栏项对应于管理每个选项卡的视图堆栈的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];
}
}
Run Code Online (Sandbox Code Playgroud)
此代码将删除左侧导航按钮上的"更多"功能,但不能解决维护导航堆栈的问题.所有其他标签工作正常.我可以遍历几个视图,并在离开并返回到该选项卡后保持堆栈.我知道这是一个复杂的问题,所以如果有一些方面我可以提供清晰度,请告诉我.谢谢!
这就是我最终解决这个问题的方法:
- (void) setSelectedViewController:(UIViewController *) selectedViewController {
self.viewControllers = [NSArray arrayWithObject:selectedViewController];
[super setSelectedViewController:selectedViewController];
}
Run Code Online (Sandbox Code Playgroud)
基本上,当您在UITabBarController上设置viewControllers时,来自5 on的任何选项卡都会将其导航控制器替换为moreNavigationController.因此,我动态设置viewControllers只包含我单击的选项卡.在这种情况下永远不会超过1,所以moreNavigationController不会发挥作用.
当我启动自定义控制器时,我只提供第一个选项卡作为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];
}
Run Code Online (Sandbox Code Playgroud)
为清楚起见,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;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2302 次 |
最近记录: |