简体   繁体   English

在自定义UITabBarController中抑制moreNavigationController

[英]Suppress moreNavigationController in custom UITabBarController

I have implemented a custom UITabBar solution for a project. 我为项目实现了自定义UITabBar解决方案。 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. 基本上,如果有超过5个项目,我使用scrollView,允许用户滚动其他选项卡项并禁止更多按钮。 A similar look and feel can be seen in the Weather Channel app. 在Weather Channel应用程序中可以看到类似的外观。

Each tab bar item corresponds to a UINavigationController that manages the stack of views for each tab. 每个标签栏项对应于管理每个选项卡的视图堆栈的UINavigationController。 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. 我遇到的问题是,当我有超过5个标签项时,从标签5开始不能正确维护导航堆栈。 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. 似乎每次返回到该选项卡时,moreNavigationController都会杀死导航堆栈,并且您将再次进入初始页面。

I've overridden the setSelectedViewController method as follows: 我已经重写了setSelectedViewController方法,如下所示:

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

Basically any tab from 5 on gets its navigation controller replaced by the moreNavigationController when you intiially set the viewControllers on UITabBarController. 基本上,当您在UITabBarController上设置viewControllers时,来自5 on的任何选项卡都会将其导航控制器替换为moreNavigationController。 Therefore, I dynamically set viewControllers to just contain the tab I'm clicking. 因此,我动态设置viewControllers只包含我单击的选项卡。 There never ends up being more than 1 in this case so the moreNavigationController doesn't come into play. 在这种情况下永远不会超过1,所以moreNavigationController不会发挥作用。

When I init my custom controller, I just supply the first tab as the viewControllers so the application can load. 当我启动自定义控制器时,我只提供第一个选项卡作为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];
}

For clarity, the tabBar delegate is set to this class so that it can respond to tab clicks. 为清楚起见,tabBar委托设置为此类,以便它可以响应选项卡单击。 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;
}

You may also override the moreNavigationController var of UITabBarController and return your own custom NavigaitonContorller like so: 您也可以覆盖UITabBarController的moreNavigationController var并返回您自己的自定义NavigaitonContorller,如下所示:

override var moreNavigationController: UINavigationController { return MyCustomNavController() }

worked in my case where I needed a NavigaitonController that allowed hiding the NavigationBar when scrolling. 在我的情况下工作,我需要一个NavigaitonController,允许在滚动时隐藏NavigationBar。

As for hiding the Back Button of the MoreNavigationController you can set the leftBarButtonItem of each NavigationItem to anything (even an empty UIBarButtonItem) and the back butotn of the more NavigaitonController will disappear. 至于隐藏MoreNavigationController的Back Button,你可以将每个NavigationItem的leftBarButtonItem设置为任何东西(甚至是空的UIBarButtonItem),并且更多NavigaitonController的后面按钮将消失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UITabbarController与MoreNavigationController,PoptoRootViewController问题 - UITabbarController with MoreNavigationController , PoptoRootViewController issue moreNavigationController 在 UITabBarController 中始终为零 - moreNavigationController is always nil in UITabBarController 从UITabBarController的moreNavigationController中删除rightBarButtonItem - Removing the rightBarButtonItem from moreNavigationController of UITabBarController UITabBarController的moreNavigationController和导航栏 - UITabBarController's moreNavigationController and Navigation Bar 编辑按钮未显示在UITabBarController的MoreNavigationController中 - Edit button not displayed in UITabBarController's MoreNavigationController 更改UITabBarController的moreNavigationController中的“编辑”按钮颜色? - Change 'edit' Button Color from moreNavigationController of UITabBarController? 如何禁用我的 UITabBarController 的 moreNavigationController 的某些选项卡? - How can I disable some of the tabs of the moreNavigationController of my UITabBarController? 无法设置UITabBarController的moreNavigationController的右键 - can't set right button of UITabBarController's moreNavigationController 在UITabBarController中加载自定义视图 - Loading a custom view in a UITabBarController 将故事板与自定义UITabbarController一起使用 - use storyboard with custom UITabbarController
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM