iOS: Fix ignored orientation changes in embedded mode
When running under the SwiftUI app lifecycle, the Godot view controller can be wrapped by a hosting controller that becomes the window's root view controller. In that case, orientation-related UIKit selectors must be propagated to the root controller before requesting an interface orientation update. This also fixes orientation settings not being applied correctly on iOS.
This commit is contained in:
@@ -665,11 +665,25 @@ void DisplayServerAppleEmbedded::screen_set_orientation(DisplayServerEnums::Scre
|
||||
ERR_FAIL_INDEX(p_screen, screen_count);
|
||||
|
||||
screen_orientation = p_orientation;
|
||||
if (@available(iOS 16.0, *)) {
|
||||
[GDTAppDelegateService.viewController setNeedsUpdateOfSupportedInterfaceOrientations];
|
||||
#ifdef IOS_ENABLED
|
||||
// Under the SwiftUI app lifecycle, GDTViewController is wrapped by a UIHostingController
|
||||
// that is the window's root VC. iOS queries the root VC for orientation preferences, so we
|
||||
// must install the selectors on the hosting class before requesting an orientation update.
|
||||
GDTViewController *vc = GDTAppDelegateService.viewController;
|
||||
if (!vc) {
|
||||
return;
|
||||
}
|
||||
#if !defined(VISIONOS_ENABLED)
|
||||
else {
|
||||
[vc propagateUIPreferencesToRootViewController];
|
||||
|
||||
UIViewController *rootViewController = vc.view.window.rootViewController ?: vc;
|
||||
if (@available(iOS 16.0, *)) {
|
||||
[rootViewController setNeedsUpdateOfSupportedInterfaceOrientations];
|
||||
UIWindowScene *windowScene = rootViewController.view.window.windowScene;
|
||||
if (windowScene) {
|
||||
UIWindowSceneGeometryPreferencesIOS *preferences = [[UIWindowSceneGeometryPreferencesIOS alloc] initWithInterfaceOrientations:[rootViewController supportedInterfaceOrientations]];
|
||||
[windowScene requestGeometryUpdateWithPreferences:preferences errorHandler:nil];
|
||||
}
|
||||
} else {
|
||||
[UIViewController attemptRotationToDeviceOrientation];
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -40,4 +40,8 @@
|
||||
@property(nonatomic, readonly, strong) GDTView *godotView;
|
||||
@property(nonatomic, readonly, strong) GDTKeyboardInputView *keyboardView;
|
||||
|
||||
#ifdef IOS_ENABLED
|
||||
- (void)propagateUIPreferencesToRootViewController;
|
||||
#endif
|
||||
|
||||
@end
|
||||
|
||||
@@ -254,6 +254,8 @@
|
||||
@selector(preferredScreenEdgesDeferringSystemGestures),
|
||||
@selector(prefersHomeIndicatorAutoHidden),
|
||||
@selector(prefersStatusBarHidden),
|
||||
@selector(shouldAutorotate),
|
||||
@selector(supportedInterfaceOrientations),
|
||||
};
|
||||
|
||||
for (SEL sel : selectors) {
|
||||
|
||||
Reference in New Issue
Block a user