之前導航欄隱藏一直是用的
self.navigationController.navigationBarHidden = YES;
最近突然發現項目中所有的從隱藏導航欄的界面A進入不隱藏導航欄的界面B,在從B返回A的時候導航欄會莫名其妙消失 一半,側滑返回試了下果然有問題,一頓百度后發現大牛都是用的
[self.navigationController setNavigationBarHidden:YES animated:YES];
然后果斷換掉了,這樣確實解決了這個問題,但是我這個A界面還跳到了另一個隱藏導航欄的界面C,本來用第一種方法的時候 C界面返回沒有問題,但是換成第二種方法后C界面返回A界面的時候就會瞬間顯示導航欄,看著相當的丑(就是一個bug)。
遇到這樣的問題也很摸不著頭腦,果斷一通百度,然后百度到了這樣一些代碼:
id<UIViewControllerTransitionCoordinator> tc = self.transitionCoordinator;
if(tc && [tc initiallyInteractive]) {
[tc notifyWhenInteractionEndsUsingBlock:
^(id<UIViewControllerTransitionCoordinatorContext> context) {
if([context isCancelled]) {
// do nothing!
}else{// not cancelled, do it
[self.navigationController setNavigationBarHidden:YES animated:YES];
}
}];
}else{// not interactive, do it
[self.navigationController setNavigationBarHidden:YES animated:YES];
}
并不懂是什么意思,然后就搜了下 UIViewControllerTransitionCoordinator,找到個這https://blog.csdn.net/yinxiaochong/article/details/47425055
看完依然很懵逼,算了,點進去看下API吧,這里就不再詳細描述API了(一堆英語,沒看明白就不獻丑了)。
在C界面的viewwilldisappear方法中添加了下邊:
id<UIViewControllerTransitionCoordinator> tc = self.transitionCoordinator;
if(tc && [tc initiallyInteractive]) {
[tc notifyWhenInteractionEndsUsingBlock:
^(id<UIViewControllerTransitionCoordinatorContext> context) {
@strongify(self);
if([context isCancelled]) {
// do nothing!
[self.navigationController setNavigationBarHidden:YES animated:NO];
}else{// not cancelled, do it
[tc animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
[self.navigationController setNavigationBarHidden:YES animated:NO];
} completion:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
[self.navigationController setNavigationBarHidden:NO animated:NO];
}];
}
}];
}else{// not interactive, do it
[tc animateAlongsideTransitionInView:self.view animation:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
@strongify(self);
[self.navigationController setNavigationBarHidden:YES animated:NO];
} completion:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
@strongify(self);
[self.navigationController setNavigationBarHidden:NO animated:NO];
}];
}
返回A界面走的是第二層if判斷的else里邊的代碼,就是讓它返回的時候先隱藏導航欄,返回完成后再顯示。
至于第一次if判斷的else里邊的代碼是C界面中還能跳轉到一個沒有導航欄的D界面,在跳轉過程中也不能顯示導航欄,所以做了隱藏操作,但是這個animateAlongsideTransitionInView:是個回調方法,我再D界面顯示出來的時候才會調用里邊的代碼,所以在D界面的viewwillappear方法里也要做一些操作:
id<UIViewControllerTransitionCoordinator> tc = self.transitionCoordinator;
if(tc && [tc initiallyInteractive]) {
[tc notifyWhenInteractionEndsUsingBlock:
^(id<UIViewControllerTransitionCoordinatorContext> context) {
@strongify(self);
if([context isCancelled]) {
// do nothing!
}else{// not cancelled, do it
[tc animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
@strongify(self);
[self.navigationController setNavigationBarHidden:YES animated:NO];
} completion:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
@strongify(self);
[self.navigationController setNavigationBarHidden:YES animated:NO];
}];
}
}];
}else{// not interactive, do it
[tc animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
@strongify(self);
[self.navigationController setNavigationBarHidden:YES animated:NO];
} completion:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
@strongify(self);
[self.navigationController setNavigationBarHidden:YES animated:NO];
}];
}
這樣做了之后這個部分的導航欄就沒有問題了,當然后邊還遇到了比這個還復雜的問題,但是基本上也是用的這個的解決思路給解決了。