这与我的另一个问题有关,iOS 8 + interactive transition + nav bar shown = broken? , 但不同。
在 iOS 8 上,当通过 NavigationControllerDelegate
/UIViewControllerInteractiveTransitioning
方法进行从 View A 到 View B 的交互式转换时, View A 有一个导航栏,并且 View B 没有,那么隐藏/取消隐藏导航栏的正确方法是什么?
我试着在 ViewController 中这样做:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[self transitionCoordinator] animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
if (self.navigationController) {
[self.navigationController setNavigationBarHidden:YES animated:animated];
}
} completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
NSArray *debugViews = [context containerView].subviews;
NSLog(@"%@", debugViews);
if ([context isCancelled] ) {
if( self.navigationController ) {
[self.navigationController setNavigationBarHidden:NO animated:animated];
}
}
}];
}
- (void)viewWillDisappear:(BOOL)animated {
[[self transitionCoordinator] animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
if (self.navigationController) {
[self.navigationController setNavigationBarHidden:NO animated:animated];
}
} completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
if ([context isCancelled] ) {
if( self.navigationController ) {
[self.navigationController setNavigationBarHidden:YES animated:animated];
}
}
}];
[super viewWillDisappear:animated];
}
...但是有两个大问题:
当动画完成时, View (主要是导航栏)有时会“闪烁”。如果你在下面有一个复杂的 View ,这真的很难看。
如果用户取消交互式转换(即没有拖得足够远或捏得不够多),那么导航栏就会永远消失,即使我可以在代码中看到它被告知要取消隐藏。
这里有一些源代码来展示这种情况:https://github.com/xaphod/UIViewControllerTransitionTut
--> 松开从一个 View Controller 转到另一个 View Controller ;第一个 View 有一个导航栏,第二个没有。完成过渡后,您有时会看到闪烁(上面的问题 1)。当您稍微松开并放开时,这就是取消的过渡:虽然您仍在 View 1 上,但导航栏已经消失(上面的问题 2)。
最佳答案
隐藏导航栏的正确方法是使用导航的 Controller 委托(delegate),确保在使用以下委托(delegate)方法之前将窗口的导航 Controller 委托(delegate)设置为自身:-
只需在 AppDelegate.m 中执行此操作
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window.rootViewController.navigationController.delegate=self;
//do your rest code...
}
-(void)navigationController:(UINavigationController *)navController
willShowViewController:(UIViewController *)viewController
animated:(BOOL)animated
{
[navController setNavigationBarHidden:([viewController isKindOfClass:[CustomViewController class]])
animated:animated]; // just mention the view controller class type for which you want to hide
}
引用自此SFO's
关于ios - 在通过 NavigationControllerDelegate 进行交互式转换期间隐藏/取消隐藏导航栏的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31539492/