ADHBaseVC.swift 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //
  2. // ADHBaseVC.swift
  3. // ADHTuanCan
  4. //
  5. // Created by 敖德亨 on 2023/9/30.
  6. //
  7. import UIKit
  8. import RxSwift
  9. import RxCocoa
  10. import IQKeyboardManagerSwift
  11. //import MJRefresh
  12. class ADHBaseVC: UIViewController {
  13. /// 导航栏
  14. var navBar: MCNavBarDelegate?{
  15. didSet{
  16. self.addNavBar()
  17. }
  18. }
  19. /// 内容距离顶部的高度约束
  20. var navBarHeightConstraint: NSLayoutConstraint?{
  21. didSet{
  22. if let navBarHeightConstraint = navBarHeightConstraint {
  23. self.navBar?.navBarHeightSubject.bind(to: navBarHeightConstraint.rx.constBind).disposed(by: disposeAutoRelease)
  24. }
  25. }
  26. }
  27. /// 设置空数据scrollView
  28. var emptyDataScrollView : UIScrollView?{
  29. didSet{
  30. self.setEmptyDataScrollView()
  31. }
  32. }
  33. /// hud 提示
  34. // lazy var hud : MCHud! = {
  35. // return MCHud()
  36. // }()
  37. lazy var hud : MCHud! = {
  38. return MCHud()
  39. }()
  40. /// 自动释放
  41. lazy var disposeAutoRelease = {
  42. return DisposeBag()
  43. }()
  44. @objc dynamic func navBack(){
  45. self.navigationController?.popViewController(animated: true)
  46. }
  47. //MARK: -
  48. override func viewDidLoad() {
  49. super.viewDidLoad()
  50. self.view.backgroundColor = kTBackgroundColor
  51. IQKeyboardManager.shared.shouldResignOnTouchOutside = true
  52. self.navigationController?.navigationBar.isHidden = true
  53. self.view.backgroundColor = kTBackgroundColor
  54. self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false
  55. }
  56. override func viewWillDisappear(_ animated: Bool) {
  57. super.viewWillDisappear(animated)
  58. // self.sx_disableInteractivePop = false
  59. }
  60. deinit {
  61. NotificationCenter.default.removeObserver(self)
  62. self.emptyDataScrollView?.emptyDataSetSource = nil
  63. self.emptyDataScrollView?.emptyDataSetDelegate = nil
  64. self.emptyDataScrollView = nil
  65. //处理定时器的释放
  66. Mirror(reflecting: self).children.forEach { (child) in
  67. if let _ = child.label{
  68. if child.value is DispatchSourceTimer{
  69. (child.value as? DispatchSourceTimer)?.cancel()
  70. }
  71. }
  72. }
  73. DLog("\(self) dealloc")
  74. }
  75. }
  76. //MARK: - Private Method
  77. extension ADHBaseVC{
  78. /// 添加navBar
  79. private func addNavBar(){
  80. for view in self.view.subviews{
  81. if view is MCNavBarDelegate{
  82. view.removeFromSuperview()
  83. break
  84. }
  85. }
  86. if let navBar = navBar {
  87. self.view.addSubview(navBar)
  88. navBar.navBarBackBlock = {[unowned self] in
  89. self.navBack()
  90. }
  91. navBar.mas_makeConstraints { make in
  92. make?.left.top()?.right()?.mas_equalTo()(self.view)
  93. make?.height.offset()(navBar.navBarHeight())
  94. }
  95. _ = navBar.navBarHeightSubject.takeUntil(self.rx.deallocated).subscribe(onNext:{_ in
  96. navBar.mas_updateConstraints { make in
  97. make?.height.offset()(navBar.navBarHeight())
  98. }
  99. })
  100. }
  101. }
  102. }
  103. //MARK: - 创建一个空数据或者错误页面
  104. extension ADHBaseVC{
  105. /// 直接显示错误界面
  106. /// - Parameter status: <#status description#>
  107. /// - Returns: <#description#>
  108. @discardableResult
  109. final func showEmptyView(_ status : MCHttpStatus = .noData) -> UIView{
  110. let emptyView = UINib.view(withType: DZNEmptyLoadView.self)!
  111. var text : String?
  112. var image : UIImage?
  113. if status == .noData{
  114. text = self.emptyTitle()
  115. }
  116. switch status{
  117. case .noData:
  118. text = self.emptyTitle()
  119. image = UIImage.init(named: self.emptyImage())
  120. case .noNetwork:
  121. text = self.noNetworkTitle()
  122. image = UIImage.init(named: self.noNetworkImage())
  123. case .error:
  124. text = self.errorTitle()
  125. image = UIImage.init(named: self.errorImage())
  126. case .timeout:
  127. text = self.networkTimeOutTitle()
  128. image = UIImage.init(named: self.failureImage())
  129. default:break
  130. }
  131. emptyView.emptyImageView.image = image
  132. emptyView.emptyLabel.text = text
  133. self.view.addSubview(emptyView)
  134. self.view.bringSubviewToFront(emptyView)
  135. emptyView.mas_makeConstraints { (make) in
  136. make?.right.left()?.bottom()?.mas_equalTo()(self.view)
  137. make?.top.mas_equalTo()(self.navBar == nil ? self.view:self.navBar!.mas_bottom)
  138. }
  139. emptyView.activity.isHidden = !(status == .normal)
  140. emptyView.emptyLabel.isHidden = (status == .normal)
  141. emptyView.emptyImageView.isHidden = (status == .normal)
  142. return emptyView
  143. }
  144. }