iOS使用UITabbarController跳转(push)界面,如何自动隐藏底部tabbar?

一、首先简单地讲一下UITabbarController的使用方法,直接上代码:

//初始化tabbarcontroller
- (void)setTabbarController{
    NSArray *array = @[contactsNav, businessNav, infoNav, myCoffeeNav];
    _tabBarController = [[UITabBarController alloc] init];
    [_tabBarController setViewControllers:array];
    //隐藏黑色线条
    [_tabBarController.tabBar setClipsToBounds:YES];
    //设置tabbar的背景颜色
    [[UITabBar appearance] setBackgroundColor:kTabbarBgColor];
    [[UITabBar appearance] setShadowImage:[[UIImage alloc] init]];
    [_tabBarController setSelectedViewController:array[0]];
    //自定义加上一条线
    UILabel *lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 0.5)];
    lineLabel.backgroundColor = HEX_COLOR(@"E5E5E5");
    [_tabBarController.tabBar addSubview:lineLabel];
    //设置item
    [self customizeTabBarForController:_tabBarController];
}
//设置tabbarItem
- (void)customizeTabBarForController:(UITabBarController *)tabBarController {
    NSInteger index = 0;
    NSArray *tabbarImages = @[@"btn_foot_friends", @"btn_foot_bussines", @"btn_foot_message", @"btn_foot_coffee"];
    for (UITabBarItem *item in [[tabBarController tabBar] items]) {
        UIImage *selectedimage = [UIImage imageNamed:[NSString stringWithFormat:@"%@_hover",tabbarImages[index]]];
        UIImage *unselectedimage = [UIImage imageNamed:[NSString stringWithFormat:@"%@",tabbarImages[index]]];
        [item setSelectedImage:[selectedimage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
        [item setImage:unselectedimage];
        CRNavigationController *nav = tabBarController.viewControllers[index];
        NSString *titleStr = [nav.viewControllers[0] title];
        item.title = titleStr;
        [item setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:HEX_COLOR(@"818C9E"),NSForegroundColorAttributeName,[UIFont systemFontOfSize:10],NSFontAttributeName, nil] forState:UIControlStateNormal];
        [item setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:HEX_COLOR(@"E24943"),NSForegroundColorAttributeName,[UIFont systemFontOfSize:10],NSFontAttributeName, nil] forState:UIControlStateSelected];
        item.titlePositionAdjustment = UIOffsetMake(0, -3);
        index++;
    }
}


二、如何自动隐藏底部的tabbar,在跳转的时候

第一步、在UIViewController的基类中添加如下代码,我使用的基类名是BaseViewController:

@implementation BaseViewController
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.hidesBottomBarWhenPushed = NO;
}
@end

第二部、在CRNavigationController(继承于UINavigationController)中重构push方法,代码如下:

@implementation CRNavigationController
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
    self.topViewController.hidesBottomBarWhenPushed = YES;
    viewController.hidesBottomBarWhenPushed = YES;
    [super pushViewController:viewController animated:animated];
}
@end

结尾:至此页面间的跳转可以自动隐藏显示底部的Tabbar

你可能感兴趣的:(iOS开发)