美文网首页Swift开发
捕获导航栏默认按钮返回或滑动返回,swift3

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

作者: 大灰很 | 来源:发表于2017-09-16 22:09 被阅读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

    相关文章

      网友评论

        本文标题:捕获导航栏默认按钮返回或滑动返回,swift3

        本文链接:https://www.haomeiwen.com/subject/godusxtx.html