今天遇到一个奇怪的bug,就是禁用UINavigationController左滑返回,怎么也没有效果。
禁用代码如下:
self.interactivePopGestureRecognizer.enabled =NO;
后来仔细搜索排查,发小有个类别实现了UINavigationController的delegate方法,并在其中打开左滑返回开关。
@interface UINavigationController (TRPopGesture)<UIGestureRecognizerDelegate,UINavigationControllerDelegate>
@end
#pragma mark - UINavigationControllerDelegate
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
// 转发给业务方代理
if (self.tr_naviDelegate && ![self.tr_naviDelegate isEqual:self]) {
if ([self.tr_naviDelegate respondsToSelector:@selector(navigationController:didShowViewController:animated:)]) {
[self.tr_naviDelegate navigationController:navigationController didShowViewController:viewController animated:animated];
}
}
// 让系统的侧滑返回生效
self.interactivePopGestureRecognizer.enabled = YES;
if (self.childViewControllers.count > 0) {
if (viewController == self.childViewControllers[0]) {
self.interactivePopGestureRecognizer.delegate = self.tr_popDelegate; // 不支持侧滑
} else {
self.interactivePopGestureRecognizer.delegate = nil; // 支持侧滑
}
}
}
这个类别是一个第三方,用来解决侧滑返回失效的问题。但每次push都会调用该delegate,无论你是否使用了这个类别。这就是系统的runtime机制导致的。
网友评论