ZCNavigationController.swift 5.25 KB
//
//  XDNavigationController.swift
//  Twear
//
//  Created by yangbin on 2021/11/16.
//
import UIKit

@objc protocol NavigationProtocol {
    /// 导航将要返回方法
    ///
    /// - Returns: true: 返回上一界面, false: 禁止返回
    @objc optional func navigationShouldPop() -> Bool
}

extension UIViewController: NavigationProtocol {
    func navigationShouldPop() -> Bool {
        return true
    }
}

class ZCNavigationController: UINavigationController {
    fileprivate var isEnableEdegePan = true
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.navigationBar.isTranslucent = false
        self.interactivePopGestureRecognizer?.delegate = self

        let navBar = UINavigationBar.appearance()
//        view.backgroundColor = .white
        let bgImage = UIImage(named: "nav_bg")
        navigationBar.setBackgroundImage(bgImage, for: .default)
        navigationBar.backgroundColor = .white
        navigationBar.isTranslucent = true
//        UINavigationBar.appearance().isTranslucent = false
        navigationBar.shadowImage = UIImage()
        
//        let statusBar = UIView(frame: CGRect(x: 0, y: -StatusBarHeight, width: SCREEN_WIDTH, height: -StatusBarHeight))
//        statusBar.backgroundColor = .white
//        navigationBar.addSubview(statusBar)
        
        navBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.rgbColorFromHex(0x050000), NSAttributedString.Key.font: BoldFont(18)]
        
        self.delegate = self
        
    }
    
    override var preferredStatusBarStyle: UIStatusBarStyle {
        return self.topViewController?.preferredStatusBarStyle ?? .default
//        return .default
    }
    
    
    /// 启用、禁用屏幕边缘侧滑手势
    func enableScreenEdgePanGestureRecognizer(_ isEnable: Bool) {
        isEnableEdegePan = isEnable
    }
    
    
    /// 获取屏幕边缘侧滑手势
    func getScreenEdgePanGestureRecognizer() -> UIScreenEdgePanGestureRecognizer? {
        
        var edgePan: UIScreenEdgePanGestureRecognizer?
        if let recognizers = view.gestureRecognizers, recognizers.count > 0 {
            for recognizer in recognizers {
                if recognizer is UIScreenEdgePanGestureRecognizer {
                    edgePan = recognizer as? UIScreenEdgePanGestureRecognizer
                    break
                }
            }
        }
        return edgePan
    }
    
    // 导航栏返回按钮点击
    @objc fileprivate func backBtnClicked() {
        
//        popViewController(animated: true)
        var shouldPop = false
        let vc: UIViewController = topViewController!
        if vc.responds(to: #selector(navigationShouldPop)) {
            shouldPop = vc.navigationShouldPop()
        }
        
        if shouldPop {
            DispatchQueue.main.async {
                if #available(iOS 13.0, *) {
                    if vc is NFCViewController {
                        //            return true
                        self.popToRootViewController(animated: true)
                        return
                    }
                } 
                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
//                    }
//                }
//            }
        }
//        if viewControllers.count == 1 { // 回到首页,打开左侧抽屉手势
//            drawerMenuController()?.needSwipeShowMenu = true
//        }
    }
}


// MARK: - delegate
extension ZCNavigationController: UINavigationControllerDelegate, UIGestureRecognizerDelegate {
    
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        if children.last is NFCViewController {
//            return true
            popToRootViewController(animated: true)
        }
        
        if !isEnableEdegePan { // 禁用边缘侧滑手势
            return false
        }
        
        
        return children.count > 1
    }
    
    func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
        
//        if viewControllers.count == 1 { // 回到首页,打开左侧抽屉手势
//            drawerMenuController()?.needSwipeShowMenu = true
//        }
    }
    
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        if viewControllers.count > 0 {


            // 隐藏tabBar底部
            viewController.hidesBottomBarWhenPushed = true
            // 关闭首页左侧抽屉手势
//            drawerMenuController()?.needSwipeShowMenu = false
//            if viewController is PYSearchViewController {
//                print("sfdsadfasf")
//
//            } else {
                //设置统一的返回按钮
                let backBtn = BackButton(target: self, action: #selector(backBtnClicked))
                viewController.navigationItem.leftBarButtonItem = UIBarButtonItem.init(customView: backBtn)
//            }
        }
        
        
        super.pushViewController(viewController, animated: animated)
    }
    
    
}