Blame view

Twear/Basic/ZCNavigationController.swift 5.25 KB
75d24c15   yangbin   123
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
  //
  //  XDNavigationController.swift
  //  Twear
  //
  //  Created by yangbin on 2021/11/16.
  //
  import UIKit
  
  @objc protocol NavigationProtocol {
      /// 导航将要返回方法
      ///
      /// - Returns: true: 返回上一界面 false: 禁止返回
      @objc optional func navigationShouldPop() -> Bool
  }
  
  extension UIViewController: NavigationProtocol {
      func navigationShouldPop() -> Bool {
          return true
      }
  }
  
  class ZCNavigationController: UINavigationController {
      fileprivate var isEnableEdegePan = true
      override func viewDidLoad() {
          super.viewDidLoad()
          
          self.navigationBar.isTranslucent = false
          self.interactivePopGestureRecognizer?.delegate = self
  
          let navBar = UINavigationBar.appearance()
  //        view.backgroundColor = .white
          let bgImage = UIImage(named: "nav_bg")
          navigationBar.setBackgroundImage(bgImage, for: .default)
          navigationBar.backgroundColor = .white
          navigationBar.isTranslucent = true
  //        UINavigationBar.appearance().isTranslucent = false
          navigationBar.shadowImage = UIImage()
          
  //        let statusBar = UIView(frame: CGRect(x: 0, y: -StatusBarHeight, width: SCREEN_WIDTH, height: -StatusBarHeight))
  //        statusBar.backgroundColor = .white
  //        navigationBar.addSubview(statusBar)
          
          navBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.rgbColorFromHex(0x050000), NSAttributedString.Key.font: BoldFont(18)]
          
          self.delegate = self
          
      }
      
      override var preferredStatusBarStyle: UIStatusBarStyle {
          return self.topViewController?.preferredStatusBarStyle ?? .default
  //        return .default
      }
      
      
      /// 启用禁用屏幕边缘侧滑手势
      func enableScreenEdgePanGestureRecognizer(_ isEnable: Bool) {
          isEnableEdegePan = isEnable
      }
      
      
      /// 获取屏幕边缘侧滑手势
      func getScreenEdgePanGestureRecognizer() -> UIScreenEdgePanGestureRecognizer? {
          
          var edgePan: UIScreenEdgePanGestureRecognizer?
          if let recognizers = view.gestureRecognizers, recognizers.count > 0 {
              for recognizer in recognizers {
                  if recognizer is UIScreenEdgePanGestureRecognizer {
                      edgePan = recognizer as? UIScreenEdgePanGestureRecognizer
                      break
                  }
              }
          }
          return edgePan
      }
      
      // 导航栏返回按钮点击
      @objc fileprivate func backBtnClicked() {
          
  //        popViewController(animated: true)
          var shouldPop = false
          let vc: UIViewController = topViewController!
          if vc.responds(to: #selector(navigationShouldPop)) {
              shouldPop = vc.navigationShouldPop()
          }
          
          if shouldPop {
              DispatchQueue.main.async {
66e7e76d   yangbin   NFC
88
89
90
91
92
93
94
                  if #available(iOS 13.0, *) {
                      if vc is NFCViewController {
                          //            return true
                          self.popToRootViewController(animated: true)
                          return
                      }
                  } 
75d24c15   yangbin   123
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
                  self.popViewController(animated: true)
              }
          } else {
  //            for subview in navigationBar.subviews {
  //                if 0.0 < subview.alpha && subview.alpha < 1.0 {
  //                    UIView.animate(withDuration: 0.25) {
  //                        subview.alpha = 1.0
  //                    }
  //                }
  //            }
          }
  //        if viewControllers.count == 1 { // 回到首页打开左侧抽屉手势
  //            drawerMenuController()?.needSwipeShowMenu = true
  //        }
      }
  }
  
  
  // MARK: - delegate
  extension ZCNavigationController: UINavigationControllerDelegate, UIGestureRecognizerDelegate {
      
      func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
66e7e76d   yangbin   NFC
117
118
119
120
          if children.last is NFCViewController {
  //            return true
              popToRootViewController(animated: true)
          }
75d24c15   yangbin   123
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
          
          if !isEnableEdegePan { // 禁用边缘侧滑手势
              return false
          }
          
          
          return children.count > 1
      }
      
      func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
          
  //        if viewControllers.count == 1 { // 回到首页打开左侧抽屉手势
  //            drawerMenuController()?.needSwipeShowMenu = true
  //        }
      }
      
      override func pushViewController(_ viewController: UIViewController, animated: Bool) {
          
          if viewControllers.count > 0 {
  
  
              // 隐藏tabBar底部
              viewController.hidesBottomBarWhenPushed = true
              // 关闭首页左侧抽屉手势
  //            drawerMenuController()?.needSwipeShowMenu = false
  //            if viewController is PYSearchViewController {
  //                print("sfdsadfasf")
  //
  //            } else {
                  //设置统一的返回按钮
                  let backBtn = BackButton(target: self, action: #selector(backBtnClicked))
                  viewController.navigationItem.leftBarButtonItem = UIBarButtonItem.init(customView: backBtn)
  //            }
          }
          
          
          super.pushViewController(viewController, animated: animated)
      }
      
      
  }