IHNavigationController.swift 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //
  2. // IHNavigationController.swift
  3. // Inhealth
  4. //
  5. // Created by weclouds on 2019/12/10.
  6. // Copyright © 2019 weclouds. All rights reserved.
  7. //
  8. import UIKit
  9. ///导航栏返回协议
  10. @objc protocol NavigationProtocol{
  11. /// 导航栏将要返回的方法
  12. ///
  13. /// - Returns : true: 返回上一界面 ,false: 禁止返回
  14. @objc optional func navigationShouldPopMethod() -> Bool
  15. }
  16. class IHNavigationController: UINavigationController {
  17. override func viewDidLoad() {
  18. super.viewDidLoad()
  19. setNavigationBar()
  20. interactivePopGestureRecognizer?.delegate = self
  21. }
  22. func setNavigationBar() {
  23. let apperance = UINavigationBar.appearance()
  24. //设置半透明效果
  25. self.navigationBar.isTranslucent = false
  26. //导航栏上按钮颜色
  27. apperance.tintColor = UIColor.black
  28. //设置导航栏颜色
  29. apperance.barTintColor = UIColor(hexString: "#FFFFFF")
  30. //去除分割线
  31. // apperance.setBackgroundImage(UIImage(), for: .default)
  32. apperance.shadowImage = UIImage()
  33. let dict = [NSAttributedString.Key.foregroundColor:UIColor.white,NSAttributedString.Key.font:UIFont(name: PingFangSC_Semibold, size: 18) ]
  34. apperance.titleTextAttributes = dict as Any as? [NSAttributedString.Key : Any]
  35. let backAppearance = UIBarButtonItem.appearance()
  36. //设置导航栏返回按钮
  37. self.navigationBar.topItem?.title = ""
  38. if #available(iOS 11, *){
  39. // backAppearance.setBackButtonTitlePositionAdjustment(UIOffset(horizontal: -200, vertical: 0), for: UIBarMetrics.default)
  40. //
  41. // let backButtonImage = UIImage(named: "返回")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
  42. //
  43. // apperance.backIndicatorImage = backButtonImage
  44. // apperance.backIndicatorTransitionMaskImage = backButtonImage
  45. let backButtonImage = UIImage(named: "返回")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
  46. apperance.backIndicatorImage = backButtonImage
  47. apperance.backIndicatorTransitionMaskImage = backButtonImage
  48. let leftBtn = UIBarButtonItem.init(image: backButtonImage, style: .plain, target: self, action: #selector(clickBack))
  49. self.navigationItem.leftBarButtonItem = leftBtn
  50. }else {
  51. //返回按钮没有文字
  52. backAppearance.setBackButtonTitlePositionAdjustment(UIOffset(horizontal: -200, vertical: 0), for: UIBarMetrics.default)
  53. let backButtonImage = UIImage(named: "返回")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
  54. backAppearance.setBackButtonBackgroundImage(backButtonImage?.resizableImage(withCapInsets: UIEdgeInsets(top: 0, left: 32, bottom: 0, right: 0)), for: UIControl.State.normal, barMetrics: UIBarMetrics.default)
  55. }
  56. }
  57. override func pushViewController(_ viewController: UIViewController, animated: Bool) {
  58. if self.viewControllers.count > 0 {
  59. viewController.hidesBottomBarWhenPushed = true
  60. viewController.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: UIBarButtonItem.Style.plain, target: self, action: #selector(IHNavigationController.backAction ))
  61. }
  62. super.pushViewController(viewController, animated: animated)
  63. }
  64. @objc func backAction() {
  65. self.navigationController?.popViewController(animated: true)
  66. }
  67. @objc private func clickBack(){
  68. }
  69. }
  70. extension UIViewController :NavigationProtocol{
  71. func navigationShouldPopMethod() -> Bool {
  72. return true
  73. }
  74. }
  75. extension UINavigationController:UINavigationBarDelegate,UIGestureRecognizerDelegate{
  76. public func navigationBar(_ navigationBar: UINavigationBar, shouldPop item: UINavigationItem) -> Bool {
  77. if viewControllers.count < navigationBar.items!.count {
  78. return true
  79. }
  80. var shouldPop = false
  81. let vc: UIViewController = topViewController!
  82. if vc.responds(to: #selector(navigationShouldPopMethod)) {
  83. shouldPop = vc.navigationShouldPopMethod()
  84. }
  85. if shouldPop {
  86. DispatchQueue.main.async {
  87. self.popViewController(animated: true)
  88. }
  89. }else{
  90. for subview in navigationBar.subviews {
  91. if 0.0 < subview.alpha && subview.alpha < 1.0 {
  92. UIView.animate(withDuration: 0.25) {
  93. subview.alpha = 1.0
  94. }
  95. }
  96. }
  97. }
  98. return false
  99. }
  100. public func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
  101. if children.count == 1 {
  102. return false
  103. }else{
  104. if topViewController?.responds(to: #selector(navigationShouldPopMethod)) != nil {
  105. return topViewController!.navigationShouldPopMethod()
  106. }
  107. return true
  108. }
  109. }
  110. open override var preferredStatusBarStyle: UIStatusBarStyle{
  111. return self.topViewController!.preferredStatusBarStyle
  112. }
  113. open override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation{
  114. return .none
  115. }
  116. open override var prefersStatusBarHidden: Bool{
  117. return false
  118. }
  119. }