在自定义UITabBarController中禁止moreNavigationController [英] Suppress moreNavigationController in custom UITabBarController

查看:197
本文介绍了在自定义UITabBarController中禁止moreNavigationController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为项目实现了自定义的UITabBar解决方案.本质上,如果有5个以上的项目,我将使用scrollView,该滚动视图将允许用户滚动浏览其他选项卡项目并取消显示more按钮.在天气频道"应用程序中可以看到类似的外观.

I have implemented a custom UITabBar solution for a project. Essentially, if there are more than 5 items, I use a scrollView that will allow the user to scroll through the additional tab items and suppresses the more button. A similar look and feel can be seen in the Weather Channel app.

每个选项卡栏项目都对应于一个UINavigationController,该UINavigationController管理每个选项卡的视图堆栈.我遇到的问题是,当我有5个以上的选项卡项目时,从选项卡5开始无法正确维护导航堆栈.似乎每次返回到该选项卡,moreNavigationController都会杀死导航堆栈,并再次带回到初始页面.

Each tab bar item corresponds to a UINavigationController that manages the stack of views for each tab. The issue I'm having is when I have more than 5 tab items, from tab 5 onward does not correctly maintain the navigation stack. It seems that the moreNavigationController kills the navigation stack each time you return to that tab and you are brought to the initial page again.

我重写了setSelectedViewController方法,如下所示:

I've overridden the setSelectedViewController method as follows:

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

此代码将删除左侧导航按钮上的更多"功能,但不能解决维护导航堆栈的问题.所有其他选项卡都可以正常工作.我可以遍历几个视图,并且在离开并返回到该选项卡后将保持堆栈.我知道这是一个复杂的问题,因此请让我知道是否有需要澄清的地方.谢谢!

This code will remove the More functionality on the left nav button but it doesn't solve the issue of maintaining the navigation stack. All other tabs work fine. I can traverse down several views and the stack is maintained after I leave and return to that tab. I understand that this is a complicated issue so please let me know if there are areas where I can provide clarity. Thanks!

推荐答案

这就是我最终解决此问题的方式:

This is how I ended up fixing this:

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

基本上,当您在UITabBarController上初始设置viewControllers时,从5开始的任何选项卡都将其导航控制器替换为moreNavigationController.因此,我动态地将viewControllers设置为仅包含我单击的选项卡.在这种情况下,永远不会超过1,因此moreNavigationController不会起作用.

Basically any tab from 5 on gets its navigation controller replaced by the moreNavigationController when you intiially set the viewControllers on UITabBarController. Therefore, I dynamically set viewControllers to just contain the tab I'm clicking. There never ends up being more than 1 in this case so the moreNavigationController doesn't come into play.

当我初始化自定义控制器时,我只提供第一个选项卡作为viewControllers,以便应用程序可以加载.

When I init my custom controller, I just supply the first tab as the viewControllers so the application can load.

- (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委托设置为此类,以便它可以响应选项卡的单击.委托方法如下:

For clarity, the tabBar delegate is set to this class so that it can respond to tab clicks. The delegate method is as follows:

- (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;
}

这篇关于在自定义UITabBarController中禁止moreNavigationController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆