BaseViewController.swift 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. /// There is a bug when trying to go back to previous view controller in a navigation controller
  18. /// on iOS 11, a scroll view in the previous screen scrolls weirdly. In order to get this fixed,
  19. /// we have to set the scrollView's `contentInsetAdjustmentBehavior` property to `.never` on
  20. /// `viewWillAppear()` and set back to the original value on `viewDidAppear()`.
  21. private var scrollViewOriginalContentInsetAdjustmentBehaviorRawValue: Int?
  22. // MARK: Initializing
  23. init() {
  24. super.init(nibName: nil, bundle: nil)
  25. }
  26. init(nibName:String) {
  27. super.init(nibName: nibName, bundle: nil)
  28. }
  29. required convenience init?(coder aDecoder: NSCoder) {
  30. self.init()
  31. }
  32. deinit {
  33. log.verbose("DEINIT: \(self.className)")
  34. }
  35. // MARK: Rx
  36. var disposeBag = DisposeBag()
  37. // MARK: View Lifecycle
  38. override func viewWillAppear(_ animated: Bool) {
  39. super.viewWillAppear(animated)
  40. // fix iOS 11 scroll view bug
  41. if #available(iOS 11, *) {
  42. if let scrollView = self.view.subviews.first as? UIScrollView {
  43. self.scrollViewOriginalContentInsetAdjustmentBehaviorRawValue =
  44. scrollView.contentInsetAdjustmentBehavior.rawValue
  45. scrollView.contentInsetAdjustmentBehavior = .never
  46. }
  47. }
  48. }
  49. override func viewDidAppear(_ animated: Bool) {
  50. super.viewDidAppear(animated)
  51. // fix iOS 11 scroll view bug
  52. if #available(iOS 11, *) {
  53. if let scrollView = self.view.subviews.first as? UIScrollView,
  54. let rawValue = self.scrollViewOriginalContentInsetAdjustmentBehaviorRawValue,
  55. let behavior = UIScrollView.ContentInsetAdjustmentBehavior(rawValue: rawValue) {
  56. scrollView.contentInsetAdjustmentBehavior = behavior
  57. }
  58. }
  59. }
  60. // MARK: Layout Constraints
  61. private(set) var didSetupConstraints = false
  62. override func viewDidLoad() {
  63. self.view.setNeedsUpdateConstraints()
  64. self.edgesForExtendedLayout = []
  65. self.view.backgroundColor = .db_gray
  66. }
  67. override func updateViewConstraints() {
  68. if !self.didSetupConstraints {
  69. self.setupConstraints()
  70. self.didSetupConstraints = true
  71. }
  72. super.updateViewConstraints()
  73. }
  74. func setupConstraints() {
  75. // Override point
  76. }
  77. }