From a00a075a3f8cd57316c67d5efe8fbea683c6df3c Mon Sep 17 00:00:00 2001 From: kirillzyusko Date: Thu, 9 Mar 2023 13:04:29 +0400 Subject: [PATCH] fix: use new rotation API for iOS 16 --- ios/RNSScreenWindowTraits.mm | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/ios/RNSScreenWindowTraits.mm b/ios/RNSScreenWindowTraits.mm index 5b1bdc9b09..79be787ea3 100644 --- a/ios/RNSScreenWindowTraits.mm +++ b/ios/RNSScreenWindowTraits.mm @@ -174,8 +174,27 @@ + (void)enforceDesiredDeviceOrientation } } if (newOrientation != UIInterfaceOrientationUnknown) { - [[UIDevice currentDevice] setValue:@(newOrientation) forKey:@"orientation"]; - [UIViewController attemptRotationToDeviceOrientation]; + if (@available(iOS 16.0, *)) { + NSArray *array = [[[UIApplication sharedApplication] connectedScenes] allObjects]; + UIWindowScene *scene = (UIWindowScene *)array[0]; + UIWindowSceneGeometryPreferencesIOS *geometryPreferences = + [[UIWindowSceneGeometryPreferencesIOS alloc] initWithInterfaceOrientations:orientationMask]; + [scene requestGeometryUpdateWithPreferences:geometryPreferences + errorHandler:^(NSError *_Nonnull error){ + }]; + + // `attemptRotationToDeviceOrientation` is deprecated for modern OS versions + // so we need to use `setNeedsUpdateOfSupportedInterfaceOrientations` + UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController; + while (topController.presentedViewController) { + topController = topController.presentedViewController; + } + + [topController setNeedsUpdateOfSupportedInterfaceOrientations]; + } else { + [[UIDevice currentDevice] setValue:@(newOrientation) forKey:@"orientation"]; + [UIViewController attemptRotationToDeviceOrientation]; + } } }); #endif