Swift开发

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

2017-09-16  本文已影响23人  大灰很
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

上一篇下一篇

猜你喜欢

热点阅读