项目优化--导航栏标签栏

前言

开发了一年的项目准备优化,现在整理下,在尽量不更换大东西的情况下修改代码,由于项目之前创建了基类,现在优化起来还是挺简单的

操作方法

隐藏标签栏的正确姿势(在导航栏控制器中)

/**重写push方法,让二级界面都没有标签栏*/
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (self.viewControllers.count > 0) {
        viewController.hidesBottomBarWhenPushed = YES;
    }
    [super pushViewController:viewController animated:animated];
}

隐藏导航栏

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //先设置当前控制器为代理
    self.navigationController.delegate = self;
}
//执行的代理方法
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    
    //需要隐藏导航栏的控制器
    BOOL hidden = ![self classNameArray:@[@"MouoMeUserCenterController",@"KSPhotoBrowser",@"ChatRedPacketController",@"MerchantDetailController",@"MouoGroupDetailsController",@"MouoGroupModifyController",@"MouoLiveRoomControler",@"NearbyTimerController",@"NearbyTimeUserInfoController",@"PersonalDetailController"]];
    [self.navigationController setNavigationBarHidden:hidden animated:YES];
}
//判断当前控制器是否是数组中的名字
- (BOOL)classNameArray:(NSArray *)array {
    BOOL reslut = YES;
    for (NSString *string in array) {
        const char *className = [string cStringUsingEncoding:NSASCIIStringEncoding];
        Class newClass = objc_getClass(className);
        if ([self isKindOfClass:newClass]) {
            reslut = NO;
        }
    }
    return reslut;
}

至于隐藏导航栏后不能右滑返回我则是用的UIViewController+BackButtonHandler

使用它的时候也遇到过一些问题,在二级页面右滑返回一级界面的时候若一级界面有点击事件有可能触发然后造成界面卡死,需要在一级界面加上禁止返回手势的操作

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    // 禁用返回手势
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    }
}

总结

为什么不用第三方的呢,确实,我在网上也看到很多别人写的代码很不错,但若用在我的项目中就会改很多东西
比如RTRootNavigationController,SmoothNavDemo,JTNavigationController都是很不错的框架,但在用他们的时候最好在项目开始的时候就使用,不然后面就只能参考下然后根据自己的实际情况使用

推荐阅读更多精彩内容