BaseViewController.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //
  2. // BaseViewController.swift
  3. // JSJP_Student_sw
  4. //
  5. // Created by Ning.ge on 2023/5/30.
  6. //
  7. import UIKit
  8. import RxSwift
  9. import RxCocoa
  10. import ReactorKit
  11. import RxDataSources
  12. class BaseViewController: UIViewController, NetAnimationLoadable {
  13. public struct Metric {
  14. static let statusBarHeight = isIphoneX ? 44.f : 20.f
  15. static let navBarHeight = 44.f
  16. }
  17. // MARK:UI
  18. let emptyView = EmptyView.loadFromNib()
  19. @IBOutlet weak var nav_top_layout: NSLayoutConstraint!
  20. @IBOutlet weak var nav_topH_layout: NSLayoutConstraint!
  21. // MARK: Properties
  22. lazy private(set) var className: String = {
  23. return type(of: self).description().components(separatedBy: ".").last ?? ""
  24. }()
  25. @IBAction func actionBackdo(_ sender: Any) {
  26. if let presentingViewController = self.presentingViewController {
  27. // 视图控制器是通过 presentViewController 呈现的
  28. if self.navigationController?.viewControllers.count == 1 {
  29. self.dismiss(animated: true)
  30. return
  31. }
  32. }
  33. self.navigationController?.popViewController(animated: true)
  34. }
  35. /// There is a bug when trying to go back to previous view controller in a navigation controller
  36. /// on iOS 11, a scroll view in the previous screen scrolls weirdly. In order to get this fixed,
  37. /// we have to set the scrollView's `contentInsetAdjustmentBehavior` property to `.never` on
  38. /// `viewWillAppear()` and set back to the original value on `viewDidAppear()`.
  39. private var scrollViewOriginalContentInsetAdjustmentBehaviorRawValue: Int?
  40. // MARK: Initializing
  41. init() {
  42. super.init(nibName: nil, bundle: nil)
  43. }
  44. init(nibName:String) {
  45. super.init(nibName: nibName, bundle: nil)
  46. }
  47. required convenience init?(coder aDecoder: NSCoder) {
  48. self.init()
  49. }
  50. deinit {
  51. NotificationCenter.default.removeObserver(self)
  52. log.verbose("DEINIT: \(self.className)")
  53. }
  54. // MARK: Rx
  55. var disposeBag = DisposeBag()
  56. // MARK: View Lifecycle
  57. override func viewWillAppear(_ animated: Bool) {
  58. super.viewWillAppear(animated)
  59. navigationController?.navigationBar.isHidden = true
  60. // fix iOS 11 scroll view bug
  61. if #available(iOS 11, *) {
  62. if let scrollView = self.view.subviews.first as? UIScrollView {
  63. self.scrollViewOriginalContentInsetAdjustmentBehaviorRawValue =
  64. scrollView.contentInsetAdjustmentBehavior.rawValue
  65. scrollView.contentInsetAdjustmentBehavior = .never
  66. }
  67. }
  68. }
  69. // //状态栏颜色
  70. // override var preferredStatusBarStyle: UIStatusBarStyle{
  71. // return .lightContent
  72. // }
  73. override func viewDidAppear(_ animated: Bool) {
  74. super.viewDidAppear(animated)
  75. // fix iOS 11 scroll view bug
  76. if #available(iOS 11, *) {
  77. if let scrollView = self.view.subviews.first as? UIScrollView,
  78. let rawValue = self.scrollViewOriginalContentInsetAdjustmentBehaviorRawValue,
  79. let behavior = UIScrollView.ContentInsetAdjustmentBehavior(rawValue: rawValue) {
  80. scrollView.contentInsetAdjustmentBehavior = behavior
  81. }
  82. }
  83. }
  84. // MARK: Layout Constraints
  85. private(set) var didSetupConstraints = false
  86. override func viewDidLoad() {
  87. self.view.setNeedsUpdateConstraints()
  88. self.edgesForExtendedLayout = []
  89. }
  90. override func updateViewConstraints() {
  91. if !self.didSetupConstraints {
  92. self.setupConstraints()
  93. self.didSetupConstraints = true
  94. }
  95. super.updateViewConstraints()
  96. }
  97. func setupConstraints() {
  98. // Override point
  99. }
  100. }