美文网首页
导航栏设置透明背景及整体透明及渐变设置

导航栏设置透明背景及整体透明及渐变设置

作者: 然亦伞 | 来源:发表于2017-12-22 16:03 被阅读306次

Tip0、设置导航栏下tableView置顶

if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
        self.tableView.scrollIndicatorInsets = self.tableView.contentInset;
    }else{
        self.automaticallyAdjustsScrollViewInsets = NO;//取消自动滚动调整,默认为YES
    }

Tip1、去除渐变过程中导航栏出现时view自动调整contentInset的Top问题

    self.extendedLayoutIncludesOpaqueBars = YES;

Tip2、设置UIImage的渲染模式(保持UIImage的原色不变)

UIImage *image = [UIImage imageNamed:@"back"];
image = [image imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal];

Tip3、添加观察者,观察tableView的滑动

[self.tableView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil];
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
    
    if ([keyPath isEqualToString:@"contentOffset"]) {
        CGFloat y = [change[@"new"] CGPointValue].y;
        CGFloat alpha = 0;
        if (y > 160.0) {
            alpha = 1.0;
        }else if (y > 0){
            alpha = y/160.0;
        }
        self.navigationController.navigationBar.alpha = alpha;
    }
}

Tip4

#pragma mark - UINavigationControllerDelegate
// 将要显示控制器
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    // 判断要显示的控制器是否是自己
    if ([viewController isKindOfClass:[self class]] or [viewController isKindOfClass:[PersonalInfoNewViewController class]] or [viewController isKindOfClass:[YiUnionMerchantHomeVC class]]) {
        
    }else{
        
    }
}

一、隐藏导航栏

- (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];
}

二、导航栏透明背景 (后使用github上WRNavigationBar)

提醒:self.navigationController.navigationBar.translucent 的值是系统自动判定的,最好不要人为强行设为YES/NO,不然会发生各种导航栏问题

1、设置导航栏背景透明

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear: animated];
    
    //添加观察者,观察tableView的滑动
    [self.tableView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil];
    
    //让tableView在导航栏控制器里置顶显示
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
        self.tableView.scrollIndicatorInsets = self.tableView.contentInset;
        
    }else{
        self.automaticallyAdjustsScrollViewInsets = NO;//取消自动滚动调整,默认为YES
    }
    
    //解决导航栏出现时tableview的contentInset的top自动调整问题
    self.extendedLayoutIncludesOpaqueBars = YES;

}

2、根据滑动改变背景色

#pragma mark 监听者
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
    
    if ([keyPath isEqualToString:@"contentOffset"]) {
        CGFloat y = [change[@"new"] CGPointValue].y;
        CGFloat alpha = 0;
        CGFloat h = 146-64;
        if (y > h) {
            
            self.navigationController.navigationBar.shadowImage = [CustomerToolModel imageWithColor:[UIColor colorWithRed:220/255.0f green:220/255.0f blue:220/255.0f alpha:1] size:CGSizeMake(self.view.frame.size.width, 0.5)];

            self.navigationItem.leftBarButtonItem.image = [[UIImage imageNamed:@"back_normal_icon"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            
            self.navigationItem.title = AppLocalizedString(@"Profile");
            
            self.navigationController.navigationBar.translucent = NO;
            
        }else{
            
            self.navigationItem.leftBarButtonItem.image = [[UIImage imageNamed:@"back_main_icon"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            
            self.navigationItem.title = nil;

            // 设置导航栏半透明
            self.navigationController.navigationBar.translucent = YES;

            //去除导航栏下边线条
            self.navigationController.navigationBar.shadowImage = [UIImage new];
        }
    }
}

三、导航栏整体透明与渐变

1、初始默认为透明

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear: animated];
   self.navigationController.navigationBar.alpha = 0;
}
- (void)viewWillDisAppear:(BOOL)animated {
    [super viewWillDisAppear: animated];
   self.navigationController.navigationBar.alpha = 1;
}

http://www.chjsun.top/2017/09/28/iOS11%E9%80%82%E9%85%8D%E9%81%87%E5%88%B0%E7%9A%84%E9%97%AE%E9%A2%98%E5%8F%8A%E5%85%B6%E8%A7%A3%E5%86%B3%E5%8A%9E%E6%B3%95/

相关文章

网友评论

      本文标题:导航栏设置透明背景及整体透明及渐变设置

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