捕获导航栏默认按钮返回或滑动返回,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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容