//
//  CustomNavigationController.swift
//  DYZB
//
//  Created by 1 on 16/10/14.
//  Copyright © 2016年 小码哥. All rights reserved.
//

import UIKit
public protocol BsNavigationControllerDelegate : NSObject {
    func canBack() -> Bool
}

class YHNavigationController: UINavigationController {
    weak var myDelegate : BsNavigationControllerDelegate?

    override func viewDidLoad() {
        super.viewDidLoad()
        self.interactivePopGestureRecognizer?.delegate = self
    }

    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        // 隐藏要push的控制器的tabbar
        if animated == true {
            viewController.hidesBottomBarWhenPushed = true
        }
        super.pushViewController(viewController, animated: animated)
    }
}

// MARK: - UIGestureRecognizerDelegate, UINavigationBarDelegate
extension YHNavigationController : UIGestureRecognizerDelegate,UINavigationBarDelegate {
    //这个方法是在手势将要激活前调用：返回YES允许右滑手势的激活，返回NO不允许右滑手势的激活
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        print(#function)
        if let returnValue = myDelegate?.canBack(){
            return returnValue
        } else {
#if DEBUG
            print(#function)
            print("myDelegate == nil 或者其他")
#endif
        }
        return true
    }


    public func navigationBar(_ navigationBar: UINavigationBar, shouldPop item: UINavigationItem) -> Bool {
        if let returnValue = myDelegate?.canBack(){
            return returnValue
        }
        return true
    }
}
