捕获导航栏默认按钮返回或滑动返回,swift3

protocol BackButtonDelegate {
    func navigationShouldPopOnBackButton() -> Bool

}

extension UINavigationController: UINavigationBarDelegate  {
  
  public func navigationBar(_ navigationBar: UINavigationBar, shouldPop item: UINavigationItem) -> Bool {
    
    if viewControllers.count < (navigationBar.items?.count)! {
      return true
    }
    
    var shouldPop = true
    let vc = self.topViewController
    if let v = vc as? BackButtonDelegate {
      shouldPop = v.navigationShouldPopOnBackButton()
    }

    if shouldPop {
      DispatchQueue.main.async {
        self.popViewController(animated: true)
      }
    } else {
      for subView in navigationBar.subviews {
        if(0 < subView.alpha && subView.alpha < 1) {
          UIView.animate(withDuration: 0.25, animations: {
            subView.alpha = 1
          })
        }
      }
    }

    return false
  }
}

然后在viewController中遵循协议BackButtonDelegate

推荐阅读更多精彩内容