iOS 拦截系统导航栏返回按钮事件
当我们使用了系统的导航栏时,默认点击返回按钮是 pop 回上一个界面。但是在有时候,我们需要在点击导航栏的返回按钮时不一定要 pop 回上一界面,有时就是要返回到指定的界面
如下是两种语言的解决办法:
OC 创建一个 Category
#import <UIKit/UIKit.h> @protocol BackButtonHandlerProtocol <NSObject> @optional // Override this method in UIViewController derived class to handle 'Back' button click -(BOOL)navigationShouldPopOnBackButton; @end @interface UIViewController (BackButtonHandler) <BackButtonHandlerProtocol> @end
#import "UIViewController+BackButtonHandler.h" @implementation UIViewController (BackButtonHandler) @end @implementation UINavigationController (ShouldPopOnBackButton) - (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item { if([self.viewControllers count] < [navigationBar.items count]) { return YES; } BOOL shouldPop = YES; UIViewController* vc = [self topViewController]; if([vc respondsToSelector:@selector(navigationShouldPopOnBackButton)]) { shouldPop = [vc navigationShouldPopOnBackButton]; } if(shouldPop) { dispatch_async(dispatch_get_main_queue(), ^{ [self popViewControllerAnimated:YES]; }); } else { // Workaround for iOS7.1. Thanks to @boliva - http://stackoverflow.com/posts/comments/34452906 for(UIView *subview in [navigationBar subviews]) { if(0. < subview.alpha && subview.alpha < 1.) { [UIView animateWithDuration:.25 animations:^{ subview.alpha = 1.; }]; } } } return NO; } @end
在需要拦截的类中重写navigationShouldPopOnBackButton这个方法即可
swift 为了不影响代码混乱,写了一个扩展类
import UIKit /// 导航返回协议 @objc protocol NavigationProtocol { /// 导航将要返回方法 /// /// - Returns: true: 返回上一界面, false: 禁止返回 @objc optional func navigationShouldPopMethod() -> Bool } extension UIViewController: NavigationProtocol { func navigationShouldPopMethod() -> Bool { return true } } extension UINavigationController: UINavigationBarDelegate, UIGestureRecognizerDelegate { public func navigationBar(_ navigationBar: UINavigationBar, shouldPop item: UINavigationItem) -> Bool { if viewControllers.count < (navigationBar.items?.count)! { return true } var shouldPop = false let vc: UIViewController = topViewController! if vc.responds(to: #selector(navigationShouldPopMethod)) { shouldPop = vc.navigationShouldPopMethod() } if shouldPop { DispatchQueue.main.async { self.popViewController(animated: true) } } else { for subview in navigationBar.subviews { if 0.0 < subview.alpha && subview.alpha < 1.0 { UIView.animate(withDuration: 0.25) { subview.alpha = 1.0 } } } } return false } public func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool { if children.count == 1 { return false } else { if topViewController?.responds(to: #selector(navigationShouldPopMethod)) != nil { return topViewController!.navigationShouldPopMethod() } return true } } }
在需要拦截的类中重写navigationShouldPopMethod这个方法即可