BaseViewController.swift 3.6 KB

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