背景
启动页是搞了一个UIWindow,在window上搞了一个UINavigationController
UINavigationController *nav = [[UINavigationController alloc] init];
window.rootViewController = nav;
启动页先执行,启动页下面的VC后执行,但是发现启动页上突然多了一个导航条,导航条已经提前通过navigationController.navigationBarHidden = YES;
隐藏了;
原因是因为在执行后面程序的时候,有个地方搞了一个runtime,里面将当前所有的UINavigationController获取了一遍,然后navigationController.navigationBarHidden = NO;
解决办法:
直接设置
navigationController.navigationBar.hidden = YES;
具体原因
设置navigationController.navigationBar.hidden = YES;
后,再设置
navigationController.navigationBarHidden = NO
就不起作用了
流程分析
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.navigationController.navigationBar.backgroundColor = [UIColor redColor];
self.navigationController.navigationBar.hidden = YES;
// Do any additional setup after loading the view.
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickTap:)];
[self.view addGestureRecognizer:tap];
[self.navigationController addObserver:self forKeyPath:@"navigationBar.hidden" options:NSKeyValueObservingOptionOld|NSKeyValueObservingOptionNew context:nil];
[self.navigationController addObserver:self forKeyPath:@"navigationBarHidden" options:NSKeyValueObservingOptionOld|NSKeyValueObservingOptionNew context:nil];
}
- (void)clickTap:(UITapGestureRecognizer *)sender {
self.navigationController.navigationBarHidden = NO;
NSLog(@"%d --- %d",self.navigationController.navigationBar.hidden, self.navigationController.navigationBarHidden);
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
NSLog(@"keyPath --- %@ --- %@",keyPath,change);
}
为了研究navigationBar.hidden
和navigationBarHidden
的关系,对navigationController
添加KVO
监听。
[self.navigationController addObserver:self forKeyPath:@"navigationBar.hidden" options:NSKeyValueObservingOptionOld|NSKeyValueObservingOptionNew context:nil];
[self.navigationController addObserver:self forKeyPath:@"navigationBarHidden" options:NSKeyValueObservingOptionOld|NSKeyValueObservingOptionNew context:nil];
当self.navigationController.navigationBar.hidden = YES
点击屏幕触发
self.navigationController.navigationBarHidden = NO;
触发KVO
navigationBarHidden
keyPath --- navigationBarHidden --- {
kind = 1;
new = 0;
old = 0;
}
最终
self.navigationController.navigationBar.hidden == 1
self.navigationController.navigationBarHidden == 0
导航条没有出现
当self.navigationController.navigationBar.hidden = YES
点击屏幕触发
self.navigationController.navigationBarHidden = YES;
触发KVO
navigationBar.hidden
navigationBarHidden
keyPath --- navigationBar.hidden --- {
kind = 1;
new = 1;
old = 1;
}
keyPath --- navigationBarHidden --- {
kind = 1;
new = 1;
old = 0;
}
最终
self.navigationController.navigationBar.hidden == 1
self.navigationController.navigationBarHidden == 1
导航条没有出现
当self.navigationController.navigationBar.hidden = NO
点击屏幕触发
self.navigationController.navigationBarHidden = YES;
触发KVO
navigationBar.hidden
navigationBarHidden
keyPath --- navigationBar.hidden --- {
kind = 1;
new = 1;
old = 0;
}
keyPath --- navigationBarHidden --- {
kind = 1;
new = 1;
old = 0;
}
最终
self.navigationController.navigationBar.hidden == 1
self.navigationController.navigationBarHidden == 1
导航条初始为显示,点击屏幕后隐藏
当self.navigationController.navigationBar.hidden = NO
点击屏幕触发
self.navigationController.navigationBarHidden = NO;
触发KVO
navigationBarHidden
keyPath --- navigationBarHidden --- {
kind = 1;
new = 0;
old = 0;
}
最终
self.navigationController.navigationBar.hidden == 1
self.navigationController.navigationBarHidden == 1
导航条初始为显示,点击屏幕为显示
总结
当
navigationController.navigationBar.hidden =YES
的时候,调用
self.navigationController.navigationBarHidden =NO/YES
导航条始终是隐藏的;
当
navigationController.navigationBar.hidden =NO
的时候,调用
self.navigationController.navigationBarHidden =YES
是通过调用navigationController.navigationBar.hidden=YES
达到修改导航条隐藏的目的;