CNLiveBaseNavigationController.swift 2.34 KB
//
//  CNLiveBaseNavigationController.swift
//  metaCode
//
//  Created by open on 2023/6/7.
//

import Foundation

class CNLiveBaseNavigationController : UINavigationController,UINavigationControllerDelegate {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //下面注释的代码会影响push ,暂时注释
//        guard let targets  = interactivePopGestureRecognizer?.value(forKey: "_targets") as? [NSObject] else {return}
//        let targetObjc = targets[0]
//        let target = targetObjc.value(forKey: "target")
//        let action = Selector(("handleNavigationTransition:"))
//        
//        let panges = UIPanGestureRecognizer(target: target, action: action)
//        view.addGestureRecognizer(panges)
        
        self.interactivePopGestureRecognizer?.delegate = self //侧滑返回
        self.delegate = self as UINavigationControllerDelegate
    }
    
//     MARK: - <UIGestureRecognizerDelegate>
//     什么时候调用:每次触发手势之前都会询问下代理,是否触发。
//     作用:拦截手势触发,手势识别器对象会调用这个代理方法来决定手势是否有效
    override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        
        return viewControllers.count > 1
    }

    func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
        
    }
    func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
        
        if navigationController.viewControllers.count > 1 {
            self.interactivePopGestureRecognizer?.isEnabled = true
        }else {
            self.interactivePopGestureRecognizer?.isEnabled = false
        }
    }

    /// 重写push方法的目的 : 拦截所有push进来的子控制器
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
            
        if (self.children.count > 0 ) {
            // 隐藏底部的工具条
            viewController.hidesBottomBarWhenPushed = true
        }
        // 所有设置搞定后, 再push控制器
        super.pushViewController(viewController, animated: animated)
    }
    
    @objc func ClickBackBtn() {
        self.popViewController(animated: true)
    }
}