iPhone:自定义标签栏没有糟糕的更多菜单

esb*_*enr 3 iphone uitabbarcontroller uitabbar

所以我想构建一个包含5个以上项目的tabbar,并且可以滚动并找到这篇文章.通过继承UITabBarController并隐藏现有的tabbar轻松完成:

for(UIView *view in self.view.subviews)
{
    if([view isKindOfClass:[UITabBar class]])
    {
        view.hidden = YES;
        break;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后我只是添加一个UIScrollView并根据tabbarcontroller的items-collection抛出一些按钮.

int i = 0;
for (UITabBarItem *item in self.tabBar.items)
{
    UIView *tab = [[[UIView alloc] initWithFrame:CGRectMake(i*60, 0, 60, 60)] autorelease];

    UIButton *btn = [[[UIButton alloc] initWithFrame:CGRectMake(7.5, 1, 45, 45)] autorelease];
    [btn setImage:item.image forState:UIControlStateNormal];
    btn.tag = i;
    [btn addTarget:self action:@selector(didSelectTabrBarItem:) forControlEvents:UIControlEventTouchUpInside];
    [tab addSubview:btn];

    [self.scrollView addSubview:tab];
    i++;

    if(self.selectedViewController == nil)
        [self setSelectedIndex:0];
}
Run Code Online (Sandbox Code Playgroud)

我重写了setSelectedindex/ViewController,因为我需要一些额外的绘图.

-(void)setSelectedViewController:(UIViewController *)selectedViewController
{
    [super setSelectedViewController:selectedViewController];
    [self startTimer];
}

-(void)setSelectedIndex:(NSUInteger)selectedIndex
{
    [super setSelectedIndex:selectedIndex];
    [self startTimer];
}
Run Code Online (Sandbox Code Playgroud)

问题是,当我按下按钮编号5,6或7时,tabbarcontroller将打开更多视图.我如何摆脱这一点,使最后三个项目像其他项目一样? - 这可能是对超级电话的打电话吗?

我的猜测是完全杀死UITabBarController并实现我自己的自定义tabbar.但是可以禁用更多菜单并让UITabBarController正常选择项目5,6和7吗?

esb*_*enr 6

所以,因为我太糟糕了,不能写一个全新的标签栏我决定调查并尝试破解UITabBarController.

这是解决方案:

实际问题是当你触摸索引大于4的标签栏项时,UITabBarController vil实际上会显示moreNavigationController.这是一个UINavigationController,包含UIMoreViewControllerList类型的视图,它是来自私有Cocoa框架的类型以及您选择的ViewController的实例.

那么我们如何摆脱更多按钮呢?

只需从moreNavigationController集合中删除UIMoreViewControllerList,只保留您选择的ViewController.

-(void)setSelectedViewController:(UIViewController *)selectedViewController
{
    [super setSelectedViewController:selectedViewController];

    if([self.moreNavigationController.viewControllers count] > 1)
    {        
        //Modify the view stack to remove the More view
        self.moreNavigationController.viewControllers = [[[NSArray alloc] initWithObjects:self.moreNavigationController.visibleViewController, nil] autorelease];
    }
}
Run Code Online (Sandbox Code Playgroud)

好吧,我们在右上角留下了一个编辑按钮(标题栏).

你怎么摆脱那个呢?

是啊.那是另一个肮脏的黑客.要删除Edit按钮,我实际上必须从UINavigationControllerDelegate为我的自定义UITabBarController上的moreNavigationController实现一个方法.

//navigationController delegate
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated 
{

    if ([navigationController isKindOfClass:NSClassFromString(@"UIMoreNavigationController")])
    {     
        // We don't need Edit button in More screen.
        UINavigationBar *morenavbar = navigationController.navigationBar;
        UINavigationItem *morenavitem = morenavbar.topItem;
        morenavitem.rightBarButtonItem = nil;
    }
}
Run Code Online (Sandbox Code Playgroud)

这就是杀掉默认更多功能的方法.我真的觉得Apple在这里生了一条裤子,创造了一个UITabBarController,它既可以处理逻辑,也可以处理UI.为什么不创建一个具有预加载ViewControllers并在之间切换的逻辑的控制器,然后如果你想要更多东西就可以使用的实现. - 甚至更好:可以禁用更多功能.