背景
啟動(dòng)頁(yè)是搞了一個(gè)UIWindow泻轰,在window上搞了一個(gè)UINavigationController
UINavigationController *nav = [[UINavigationController alloc] init];
window.rootViewController = nav;
啟動(dòng)頁(yè)先執(zhí)行,啟動(dòng)頁(yè)下面的VC后執(zhí)行且轨,但是發(fā)現(xiàn)啟動(dòng)頁(yè)上突然多了一個(gè)導(dǎo)航條浮声,導(dǎo)航條已經(jīng)提前通過navigationController.navigationBarHidden = YES;
隱藏了;
原因是因?yàn)樵趫?zhí)行后面程序的時(shí)候旋奢,有個(gè)地方搞了一個(gè)runtime泳挥,里面將當(dāng)前所有的UINavigationController獲取了一遍,然后navigationController.navigationBarHidden = NO;
解決辦法:
直接設(shè)置
navigationController.navigationBar.hidden = YES;
具體原因
設(shè)置navigationController.navigationBar.hidden = YES;
后至朗,再設(shè)置
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
的關(guān)系屉符,對(duì)navigationController
添加KVO
監(jiān)聽。
[self.navigationController addObserver:self forKeyPath:@"navigationBar.hidden" options:NSKeyValueObservingOptionOld|NSKeyValueObservingOptionNew context:nil];
[self.navigationController addObserver:self forKeyPath:@"navigationBarHidden" options:NSKeyValueObservingOptionOld|NSKeyValueObservingOptionNew context:nil];
當(dāng)self.navigationController.navigationBar.hidden = YES
點(diǎn)擊屏幕觸發(fā)
self.navigationController.navigationBarHidden = NO;
觸發(fā)KVO
navigationBarHidden
keyPath --- navigationBarHidden --- {
kind = 1;
new = 0;
old = 0;
}
最終
self.navigationController.navigationBar.hidden == 1
self.navigationController.navigationBarHidden == 0
導(dǎo)航條沒有出現(xiàn)
當(dāng)self.navigationController.navigationBar.hidden = YES
點(diǎn)擊屏幕觸發(fā)
self.navigationController.navigationBarHidden = YES;
觸發(fā)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
導(dǎo)航條沒有出現(xiàn)
當(dāng)self.navigationController.navigationBar.hidden = NO
點(diǎn)擊屏幕觸發(fā)
self.navigationController.navigationBarHidden = YES;
觸發(fā)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
導(dǎo)航條初始為顯示锹引,點(diǎn)擊屏幕后隱藏
當(dāng)self.navigationController.navigationBar.hidden = NO
點(diǎn)擊屏幕觸發(fā)
self.navigationController.navigationBarHidden = NO;
觸發(fā)KVO
navigationBarHidden
keyPath --- navigationBarHidden --- {
kind = 1;
new = 0;
old = 0;
}
最終
self.navigationController.navigationBar.hidden == 1
self.navigationController.navigationBarHidden == 1
導(dǎo)航條初始為顯示矗钟,點(diǎn)擊屏幕為顯示
總結(jié)
當(dāng)
navigationController.navigationBar.hidden =YES
的時(shí)候,調(diào)用
self.navigationController.navigationBarHidden =NO/YES
導(dǎo)航條始終是隱藏的嫌变;
當(dāng)
navigationController.navigationBar.hidden =NO
的時(shí)候吨艇,調(diào)用
self.navigationController.navigationBarHidden =YES
是通過調(diào)用navigationController.navigationBar.hidden=YES
達(dá)到修改導(dǎo)航條隱藏的目的;