UIInterface+HXRotation.m 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //
  2. // UIInterface+HXRotation.m
  3. // HXRotationTool
  4. //
  5. // Created by Mac on 2022/12/9.
  6. //
  7. #import "UIInterface+HXRotation.h"
  8. @implementation UIViewController (HXRotation)
  9. - (BOOL)hx_rotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
  10. #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 160000
  11. if (@available(iOS 16.0, *)) {
  12. [self setNeedsUpdateOfSupportedInterfaceOrientations];
  13. __block BOOL result = YES;
  14. UIInterfaceOrientationMask mask = 1 << interfaceOrientation;
  15. UIWindow *window = self.view.window ?: UIApplication.sharedApplication.delegate.window;
  16. [window.windowScene requestGeometryUpdateWithPreferences:[[UIWindowSceneGeometryPreferencesIOS alloc] initWithInterfaceOrientations:mask] errorHandler:^(NSError * _Nonnull error) {
  17. if (error) {
  18. result = NO;
  19. }
  20. }];
  21. return result;
  22. }
  23. #endif
  24. [[UIDevice currentDevice] setValue:@(interfaceOrientation) forKey:@"orientation"];
  25. [UIViewController attemptRotationToDeviceOrientation];
  26. return YES;
  27. }
  28. - (void)hx_setNeedsUpdateOfSupportedInterfaceOrientations {
  29. if (@available(iOS 16.0, *)) {
  30. dispatch_async(dispatch_get_main_queue(), ^{
  31. #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 160000
  32. [self setNeedsUpdateOfSupportedInterfaceOrientations];
  33. #else
  34. #pragma clang diagnostic push
  35. #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
  36. SEL supportedInterfaceSelector = NSSelectorFromString(@"setNeedsUpdateOfSupportedInterfaceOrientations");
  37. [self performSelector:supportedInterfaceSelector];
  38. #pragma clang diagnostic pop
  39. #endif
  40. });
  41. }
  42. }
  43. @end
  44. // UINavigationController
  45. @implementation UINavigationController (HXRotation)
  46. - (BOOL)shouldAutorotate {
  47. return [[self.viewControllers lastObject] shouldAutorotate];
  48. }
  49. - (NSUInteger)supportedInterfaceOrientations {
  50. return [[self.viewControllers lastObject] supportedInterfaceOrientations];
  51. }
  52. - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
  53. return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
  54. }
  55. - (UIViewController *)childViewControllerForStatusBarStyle
  56. {
  57. return [self.viewControllers lastObject];
  58. }
  59. @end
  60. // UITabBarController
  61. @implementation UITabBarController (HXRotation)
  62. - (BOOL)shouldAutorotate {
  63. return [self.selectedViewController shouldAutorotate];
  64. }
  65. - (NSUInteger)supportedInterfaceOrientations {
  66. return [self.selectedViewController supportedInterfaceOrientations];
  67. }
  68. - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
  69. return [self.selectedViewController preferredInterfaceOrientationForPresentation];
  70. }
  71. @end