BaseViewController.swift 3.4 KB

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