BaseViewController.swift 3.7 KB

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