1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- //
- // UIInterface+HXRotation.m
- // HXRotationTool
- //
- // Created by Mac on 2022/12/9.
- //
- #import "UIInterface+HXRotation.h"
- @implementation UIViewController (HXRotation)
- - (BOOL)hx_rotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
-
- #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 160000
- if (@available(iOS 16.0, *)) {
- [self setNeedsUpdateOfSupportedInterfaceOrientations];
- __block BOOL result = YES;
- UIInterfaceOrientationMask mask = 1 << interfaceOrientation;
- UIWindow *window = self.view.window ?: UIApplication.sharedApplication.delegate.window;
- [window.windowScene requestGeometryUpdateWithPreferences:[[UIWindowSceneGeometryPreferencesIOS alloc] initWithInterfaceOrientations:mask] errorHandler:^(NSError * _Nonnull error) {
- if (error) {
- result = NO;
- }
- }];
- return result;
- }
- #endif
-
- [[UIDevice currentDevice] setValue:@(interfaceOrientation) forKey:@"orientation"];
- [UIViewController attemptRotationToDeviceOrientation];
-
- return YES;
- }
- - (void)hx_setNeedsUpdateOfSupportedInterfaceOrientations {
-
- if (@available(iOS 16.0, *)) {
-
- dispatch_async(dispatch_get_main_queue(), ^{
-
- #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 160000
- [self setNeedsUpdateOfSupportedInterfaceOrientations];
- #else
-
- #pragma clang diagnostic push
- #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
- SEL supportedInterfaceSelector = NSSelectorFromString(@"setNeedsUpdateOfSupportedInterfaceOrientations");
- [self performSelector:supportedInterfaceSelector];
- #pragma clang diagnostic pop
-
- #endif
- });
-
- }
- }
- @end
- // UINavigationController
- @implementation UINavigationController (HXRotation)
- - (BOOL)shouldAutorotate {
- return [[self.viewControllers lastObject] shouldAutorotate];
- }
- - (NSUInteger)supportedInterfaceOrientations {
- return [[self.viewControllers lastObject] supportedInterfaceOrientations];
- }
- - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
- return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
- }
- - (UIViewController *)childViewControllerForStatusBarStyle
- {
- return [self.viewControllers lastObject];
- }
- @end
- // UITabBarController
- @implementation UITabBarController (HXRotation)
- - (BOOL)shouldAutorotate {
- return [self.selectedViewController shouldAutorotate];
- }
- - (NSUInteger)supportedInterfaceOrientations {
- return [self.selectedViewController supportedInterfaceOrientations];
- }
- - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
- return [self.selectedViewController preferredInterfaceOrientationForPresentation];
- }
- @end
|