iOS导航栏随滑动渐隐渐现且不影响push页面导航栏的实现方式

时间:2022-05-22 03:54:41
// 自定义导航栏视图
@property ( nonatomic , strong ) UIView * naviView;

viewDidLoad中:
if ([ self respondsToSelector : @selector (setEdgesForExtendedLayout:)])
    {
        [
self setEdgesForExtendedLayout : UIRectEdgeNone ];
    }

self . naviView = [[ UIView alloc ] initWithFrame : CGRectMake ( 0 , 0 , YJScreenWidth , 64 )];
   
self . naviView . backgroundColor = THEME_Color ;
    [
self . view addSubview : self . naviView ];
    [self.view bringSubviewToFront:self.naviView];

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

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

#pragma mark - scrollViewDelegate
- ( void )scrollViewDidScroll:( UIScrollView *)scrollView
{
   
CGFloat alpha = scrollView. contentOffset . y / 100 ;
   
if (alpha >= 1 ) {
        alpha =
1 ;
    }
else if (alpha <= 0 )
    {
        alpha =
0 ;
    }
   
self . naviView . backgroundColor = [ THEME_Color colorWithAlphaComponent :alpha];
}