Recently I've got stuck in a bug of pushing UIViewController
to UINavigationViewController
, and I've found the solution for it.
Bug scenario:
it doesn't matter how you push the UIViewController
(by segue, using pushViewController, ..). In RootViewController of your NavigationViewController, try to use swipe back gesture! (nothing should happen), then try to push a ViewController into your NavigationViewController by tapping an element. => App Freezes! Here, if you capture the screen by XCode, you will find that the current visible screen is the next screen!! (but it's not!), Cpu usage and any other things are normal. you can get out of App Freezing by just another swipe back gesture!
Here is the solution:
In your BaseNavigationViewController (which extends UINavigationViewController
) insert these codes (especially when you're using UITabbarNavigationViewController
)
- (void)navigationController:(UINavigationController *)navigationController
didShowViewController:(UIViewController *)viewController
animated:(BOOL)animate {
if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
if (self.viewControllers.count > 1) {
self.interactivePopGestureRecognizer.delegate = self;
self.interactivePopGestureRecognizer.enabled = YES;
} else {
self.interactivePopGestureRecognizer.delegate = nil;
self.interactivePopGestureRecognizer.enabled = NO;
}
}
}
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
return self.viewControllers.count > 1;
}
return NO;
}
Do not forget to write these codes too:
in viewDidLoad
of BaseNavigationViewController:
self.delegate = self;
and your BaseNavigationViewController
must use these two protocols:
UINavigationControllerDelegate
, UIGestureRecognizerDelegate