O2BaseControllerForRotate.swift 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. //
  2. // O2BaseForRotateUIViewController.swift
  3. // O2Platform
  4. //
  5. // Created by FancyLou on 2021/12/29.
  6. // Copyright © 2021 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. // O2BaseForRotateUITabBarController 作为根视图,需要把参数传递给它的子视图。
  10. class O2BaseForRotateUITabBarController: UITabBarController {
  11. override var prefersStatusBarHidden: Bool {
  12. return selectedViewController?.prefersStatusBarHidden ?? kDefaultPrefersStatusBarHidden
  13. }
  14. override var preferredStatusBarStyle: UIStatusBarStyle {
  15. return selectedViewController?.preferredStatusBarStyle ?? kDefaultPreferredStatusBarStyle
  16. }
  17. override var shouldAutorotate: Bool {
  18. return selectedViewController?.shouldAutorotate ?? kDefaultShouldAutorotate
  19. }
  20. override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
  21. return [selectedViewController?.supportedInterfaceOrientations ?? kDefaultSupportedInterfaceOrientations, preferredInterfaceOrientationForPresentation.orientationMask]
  22. }
  23. override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
  24. return selectedViewController?.preferredInterfaceOrientationForPresentation ?? kDefaultPreferredInterfaceOrientationForPresentation
  25. }
  26. }
  27. // 导航试图
  28. class O2BaseForRotateUINavViewController: UINavigationController {
  29. override func viewDidLoad() {
  30. super.viewDidLoad()
  31. interactivePopGestureRecognizer?.delegate = self // 切记不要放在构造方法中配置,因为那时的 interactivePopGestureRecognizer 可能是 nil
  32. }
  33. override var shouldAutorotate: Bool {
  34. if let presentedViewController = presentedViewController, presentedViewController is UIAlertController {
  35. return false
  36. }
  37. if let presentedController = topViewController?.presentedViewController, presentedController.isBeingPresented {
  38. return presentedViewController?.shouldAutorotate ?? kDefaultShouldAutorotate
  39. }
  40. if let presentedController = topViewController?.presentedViewController, presentedController.isBeingDismissed {
  41. return topViewController?.shouldAutorotate ?? kDefaultShouldAutorotate
  42. }
  43. return visibleViewController?.shouldAutorotate ?? kDefaultShouldAutorotate
  44. }
  45. override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
  46. if let presentedViewController = presentedViewController, presentedViewController is UIAlertController {
  47. return kDefaultSupportedInterfaceOrientations
  48. }
  49. if let presentedController = topViewController?.presentedViewController, presentedController.isBeingPresented {
  50. return presentedViewController?.supportedInterfaceOrientations ?? kDefaultSupportedInterfaceOrientations
  51. }
  52. if let presentedController = topViewController?.presentedViewController, presentedController.isBeingDismissed {
  53. return topViewController?.supportedInterfaceOrientations ?? kDefaultSupportedInterfaceOrientations
  54. }
  55. return visibleViewController?.supportedInterfaceOrientations ?? kDefaultSupportedInterfaceOrientations
  56. }
  57. override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
  58. if let presentedController = topViewController?.presentedViewController, presentedController.isBeingPresented {
  59. return presentedViewController?.preferredInterfaceOrientationForPresentation ?? kDefaultPreferredInterfaceOrientationForPresentation
  60. }
  61. if let presentedController = topViewController?.presentedViewController, presentedController.isBeingDismissed {
  62. return topViewController?.preferredInterfaceOrientationForPresentation ?? kDefaultPreferredInterfaceOrientationForPresentation
  63. }
  64. return visibleViewController?.preferredInterfaceOrientationForPresentation ?? kDefaultPreferredInterfaceOrientationForPresentation
  65. }
  66. override var prefersStatusBarHidden: Bool {
  67. if let presentedController = topViewController?.presentedViewController, presentedController.isBeingPresented {
  68. return presentedViewController?.prefersStatusBarHidden ?? kDefaultPrefersStatusBarHidden
  69. }
  70. if let presentedController = topViewController?.presentedViewController, presentedController.isBeingDismissed {
  71. return topViewController?.prefersStatusBarHidden ?? kDefaultPrefersStatusBarHidden
  72. }
  73. return visibleViewController?.prefersStatusBarHidden ?? kDefaultPrefersStatusBarHidden
  74. }
  75. override var preferredStatusBarStyle: UIStatusBarStyle {
  76. if let presentedController = topViewController?.presentedViewController, presentedController.isBeingPresented {
  77. return presentedViewController?.preferredStatusBarStyle ?? kDefaultPreferredStatusBarStyle
  78. }
  79. if let presentedController = topViewController?.presentedViewController, presentedController.isBeingDismissed {
  80. return topViewController?.preferredStatusBarStyle ?? kDefaultPreferredStatusBarStyle
  81. }
  82. return visibleViewController?.preferredStatusBarStyle ?? kDefaultPreferredStatusBarStyle
  83. }
  84. }
  85. extension O2BaseForRotateUINavViewController: UIGestureRecognizerDelegate {
  86. func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
  87. if let controller = topViewController, controller.isForbidInteractivePopGesture {
  88. return false // 播放器处于横屏时,禁用左滑手势
  89. }
  90. return viewControllers.count > 1
  91. }
  92. }
  93. class O2BaseForRotateUIViewController: UIViewController {
  94. // MARK: - 关于旋转的一些配置和说明
  95. // _xxx_ 系列方法,由子类自定义实现,未实现时,使用下面的默认参数
  96. var _preferredStatusBarStyle_: UIStatusBarStyle? { return nil }
  97. var _prefersStatusBarHidden_: Bool? { return nil }
  98. var _shouldAutorotate_: Bool? { return nil }
  99. var _supportedInterfaceOrientations_: UIInterfaceOrientationMask? { return nil }
  100. var _preferredInterfaceOrientationForPresentation_: UIInterfaceOrientation? { return nil }
  101. override var preferredStatusBarStyle: UIStatusBarStyle {
  102. if let presentedController = presentedViewController, presentedController.isBeingPresented {
  103. return presentedController.preferredStatusBarStyle
  104. }
  105. if let presentedController = presentedViewController, presentedController.isBeingDismissed {
  106. return _preferredStatusBarStyle_ ?? kDefaultPreferredStatusBarStyle
  107. }
  108. if let presentedController = presentedViewController {
  109. return presentedController.preferredStatusBarStyle
  110. }
  111. return _preferredStatusBarStyle_ ?? kDefaultPreferredStatusBarStyle
  112. }
  113. override var prefersStatusBarHidden: Bool {
  114. if let presentedController = presentedViewController, presentedController.isBeingPresented {
  115. return presentedController.prefersStatusBarHidden
  116. }
  117. if let presentedController = presentedViewController, presentedController.isBeingDismissed {
  118. return _prefersStatusBarHidden_ ?? kDefaultPrefersStatusBarHidden
  119. }
  120. if let presentedController = presentedViewController {
  121. return presentedController.prefersStatusBarHidden
  122. }
  123. return _prefersStatusBarHidden_ ?? kDefaultPrefersStatusBarHidden
  124. }
  125. override var shouldAutorotate: Bool {
  126. if let presentedController = presentedViewController, presentedController.isBeingPresented {
  127. return presentedController.shouldAutorotate
  128. }
  129. if let presentedController = presentedViewController, presentedController.isBeingDismissed {
  130. return _shouldAutorotate_ ?? kDefaultShouldAutorotate
  131. }
  132. if let presentedController = presentedViewController {
  133. return presentedController.shouldAutorotate
  134. }
  135. return _shouldAutorotate_ ?? kDefaultShouldAutorotate
  136. }
  137. override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
  138. if let presentedController = presentedViewController, presentedController.isBeingPresented {
  139. return presentedController.supportedInterfaceOrientations
  140. }
  141. if let presentedController = presentedViewController, presentedController.isBeingDismissed {
  142. return _supportedInterfaceOrientations_ ?? kDefaultSupportedInterfaceOrientations
  143. }
  144. if let presentedController = presentedViewController {
  145. return presentedController.supportedInterfaceOrientations
  146. }
  147. return _supportedInterfaceOrientations_ ?? kDefaultSupportedInterfaceOrientations
  148. }
  149. override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
  150. if let presentedController = presentedViewController, presentedController.isBeingPresented {
  151. return presentedController.preferredInterfaceOrientationForPresentation
  152. }
  153. if let presentedController = presentedViewController, presentedController.isBeingDismissed {
  154. return _preferredInterfaceOrientationForPresentation_ ?? kDefaultPreferredInterfaceOrientationForPresentation
  155. }
  156. if let presentedController = presentedViewController {
  157. return presentedController.preferredInterfaceOrientationForPresentation
  158. }
  159. return _preferredInterfaceOrientationForPresentation_ ?? kDefaultPreferredInterfaceOrientationForPresentation
  160. }
  161. }