UINavigationController 返回手势与 leftBarButtonItem

UINavigationController 如果设置了当前控制器的 navigationItem.leftBarButtonItem 或 navigationItem.leftBarButtonItems,则返回手势失效。可以通过自定义 NavigationController,实现设置 navigationItem.leftBarButtonItem 或 navigationItem.leftBarButtonItems,并且保留返回手势。

UINavigationController 的 interactivePopGestureRecognizer 属性为返回手势。设置手势的 delegate,手动管理返回手势。当 UINavigationController 没有或只有一个子控制器时,返回手势不能响应,否则会出现奇怪的现象。自定义 currentNotRootVC 属性,记录当前的非根子控制器(只有一个子控制器时,此子控制器为根子控制器,root view controller)。设置 UINavigationController 的 delegate,通过 UINavigationControllerDelegate 的方法,更新 currentNotRootVC。

import UIKit
 
class NavigationController: UINavigationController, UINavigationControllerDelegate, UIGestureRecognizerDelegate {
 
    // Current child view controller, but not root view controller
    private var currentNotRootVC: UIViewController?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        delegate = self
        interactivePopGestureRecognizer?.delegate = self
    }
    
    // MARK: - UINavigationControllerDelegate
    
    func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
        currentNotRootVC = viewControllers.count <= 1 ? nil : viewController
        /*
        switch viewController {
        case is SomeVCClassForbidPopGesture:
            interactivePopGestureRecognizer?.isEnabled = false
        default:
            interactivePopGestureRecognizer?.isEnabled = true
        }
         */
    }
    
    // MARK: - UIGestureRecognizerDelegate
    
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        if interactivePopGestureRecognizer == gestureRecognizer {
            // Begin to pop only when top view controller is current child view controller but not root view controller
            return currentNotRootVC == topViewController
        }
        return true
    }
 
}

如果有部分控制器不允许返回手势(例如编辑信息,退出页面需要确认),则在func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool)方法中,加入对 viewController 的判断,开启或禁用返回手势

你可能感兴趣的:(UINavigationController 返回手势与 leftBarButtonItem)