美文网首页
handle navigationBar hide and sh

handle navigationBar hide and sh

作者: 春暖花已开 | 来源:发表于2018-09-22 00:04 被阅读11次

    我们只需要将需要进行导航栏隐藏与展示的vc放到baseNavigationController控制器下,并在该控制器的代理里去操作指定控制器的导航栏的隐藏即可,无需在控制器里做任何操作。
    具体实现如下:

    #import "BaseNavigationController.h"
    
    #import "NoNavTargetVC.h"
    
    @interface BaseNavigationController ()<UINavigationControllerDelegate, UIGestureRecognizerDelegate>
    
    @end
    
    @implementation BaseNavigationController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        self.delegate = self;
        
        id target = self.interactivePopGestureRecognizer.delegate;
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:target action:NSSelectorFromString(@"handleNavigationTransition:")];
        [self.view addGestureRecognizer:pan];
        self.interactivePopGestureRecognizer.enabled = NO;
    }
    
    - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
        if (self.viewControllers.count > 0) {
            viewController.hidesBottomBarWhenPushed = YES;
        }
        [super pushViewController:viewController animated:animated];
    }
    
    
    #pragma mark - UINavigationControllerDelegate
    
    - (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
        BOOL isHideNav = [viewController isKindOfClass:[NoNavTargetVC class]];
        
        NSLog(@"isHide = %d", isHideNav);
        [self setNavigationBarHidden:isHideNav animated:YES];
    }
    
    @end
    
    也可以这样:

    在需要隐藏的VC里重写:

    - (void)viewWillAppear:(BOOL)animated{
        [super viewWillAppear:animated];
        
        self.navigationController.navigationBarHidden = YES;
    }
    
    - (void)viewWillDisappear:(BOOL)animated{
        [super viewWillDisappear:animated];
        
        self.navigationController.navigationBarHidden = NO;
    }
    

    相关文章

      网友评论

          本文标题:handle navigationBar hide and sh

          本文链接:https://www.haomeiwen.com/subject/uurtoftx.html