iOS NavigationBar系统导航栏影藏问题都在这!

需求1:只有在首页或者某一个页面隐藏navigationController,其他页面全部正常(系统导航栏,不包括自定义导航栏).

不怎么完美的解决办法1:
1.设置透明图片....
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = [UIImage new];
2.直接设置透明度...
- (void)viewWillAppear:(BOOL)animated{
       [super viewWillAppear:animated];
       [[self.navigationController.navigationBar subviews] objectAtIndex:0].alpha = 0;
}
- (void)viewWillDisappear:(BOOL)animated{
       [super viewWillDisappear:animated];
       [[self.navigationController.navigationBar subviews] objectAtIndex:0].alpha = 1;
}
不怎么完美的解决办法2:

此方法在返回时(手势,返回按钮)会有64的白条出现

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

- (void)viewWillDisappear:(BOOL)animated {
    [self.navigationController setNavigationBarHidden:NO animated:NO];
    [super viewWillDisappear:animated];
}
解决办法3:

此方法是调用了系统动画的方式在方法1的基础上改进

- (void)viewWillAppear:(BOOL)animated {
    [self.navigationController setNavigationBarHidden:YES animated:animated];
    [super viewWillAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [self.navigationController setNavigationBarHidden:NO animated:animated];
    [super viewWillDisappear:animated];
}
解决办法4:

导航栏代理方法,将需要影藏的控制器导航栏代理设置为self,然后调用代理方法,一个代理方法,一行代码搞定

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    [self.navigationController setNavigationBarHidden:[viewController isKindOfClass:[self class]] animated:YES];
}

需求2: navigationBar随着scrollview偏移量改变透明度

不怎么完美的解决办法1:

此方法即在界面出现的变化的时候刷新自己的导航栏

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if(self.tableView.y == 0){
        self.navigationController.navigationBar.alpha = 1;
    }
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.alpha = 1;
}
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self refreshNavAlpha];
}
- (void)refreshNavAlpha{
    float alpha = _lastY / 64;
    alpha = alpha > 1 ? 1 : alpha;
    if (alpha < 0) {
        alpha = 0;
    }
    self.navigationController.navigationBar.alpha = alpha;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    _lastY = scrollView.contentOffset.y;
    [self refreshNavAlpha];
}
相对完美的解决办法:

寻找了很多APP,发现手机QQ6.7.1版本的导航栏,是采取渐变的方式,个人认为,导航栏渐变还是需要自己处理好viewWillAppear方法中进入时刷新navigationBar,可以根据方法1中的方法进行改进,将navigationBar.alpha的透明度变化,增加一个渐变动画即可,很简单,即可和手机QQ的效果一样!

- (void)refreshNavAlpha{
    float alpha = _lastY / 64;
    alpha = alpha > 1 ? 1 : alpha;
    if (alpha < 0) {
        alpha = 0;
    }
    [UIView animateWithDuration:0.2 animations:^{
        self.navigationController.navigationBar.alpha = alpha;
    }];
}

此Demo以及在后面补上

你可能感兴趣的:(iOS NavigationBar系统导航栏影藏问题都在这!)