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;
}
网友评论