BaseViewController.swift 3.1 KB

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