KVO监测tableView的滑动
首先添加监听者
[self.tableView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil];
监听属性值发生改变时回调
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{ NSLog(@"%f",self.tableView.contentOffset.y); CGFloat offset = self.tableView.contentOffset.y; CGFloat delta = offset/64.f + 0.5; NSLog(@"===============%f",offset/64.f); delta = MAX(0, delta); self.navigationController.navigationBar.alpha = MIN(delta,1); }
设置导航栏背景色
#define RGBACOLOR(r, g, b, a) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:(a)] self.navigationController.navigationBar.barTintColor = RGBACOLOR(36, 188, 61, 10); self.navigationController.navigationBar.tintColor = [UIColor whiteColor]; self.navigationController.navigationBar.barStyle = UIStatusBarStyleDefault;