當我們使用了系統的導航欄時,默認點擊返回按鈕是 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這個方法即可
