Workaround for views being in portrait instead of landscape mode on iOS 16 Fixes https://github.com/libsdl-org/SDL/issues/6289
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
diff --git a/src/video/uikit/SDL_uikitmetalview.m b/src/video/uikit/SDL_uikitmetalview.m
index 8bc3380..af6eb0c 100644
--- a/src/video/uikit/SDL_uikitmetalview.m
+++ b/src/video/uikit/SDL_uikitmetalview.m
@@ -69,6 +69,18 @@
CGSize size = self.bounds.size;
size.width *= self.layer.contentsScale;
size.height *= self.layer.contentsScale;
+
+ /* Make sure the width/height are oriented correctly
+ *
+ * This works around an issue in iOS 16 where the bounds come back in portrait mode
+ * instead of landscape until the event loop runs.
+ */
+ if ([self shouldSwapDimensions:(size.width >= size.height)]) {
+ CGFloat temp = size.width;
+ size.width = size.height;
+ size.height = temp;
+ }
+
((CAMetalLayer *)self.layer).drawableSize = size;
}
diff --git a/src/video/uikit/SDL_uikitview.h b/src/video/uikit/SDL_uikitview.h
index dcd63c7..5369bb2 100644
--- a/src/video/uikit/SDL_uikitview.h
+++ b/src/video/uikit/SDL_uikitview.h
@@ -35,6 +35,8 @@
- (void)setSDLWindow:(SDL_Window *)window;
+- (BOOL)shouldSwapDimensions:(BOOL)portrait;
+
#if !TARGET_OS_TV && defined(__IPHONE_13_4)
- (UIPointerRegion *)pointerInteraction:(UIPointerInteraction *)interaction regionForRequest:(UIPointerRegionRequest *)request defaultRegion:(UIPointerRegion *)defaultRegion API_AVAILABLE(ios(13.4));
- (UIPointerStyle *)pointerInteraction:(UIPointerInteraction *)interaction styleForRegion:(UIPointerRegion *)region API_AVAILABLE(ios(13.4));
diff --git a/src/video/uikit/SDL_uikitview.m b/src/video/uikit/SDL_uikitview.m
index 8c48cfa..50d7393 100644
--- a/src/video/uikit/SDL_uikitview.m
+++ b/src/video/uikit/SDL_uikitview.m
@@ -120,6 +120,8 @@ extern int SDL_AppleTVRemoteOpenedAsJoystick;
[data.uiwindow layoutIfNeeded];
}
+ sdlwindow = window;
+
/* Add ourself to the new window. */
if (window) {
data = (__bridge SDL_WindowData *) window->driverdata;
@@ -144,8 +146,29 @@ extern int SDL_AppleTVRemoteOpenedAsJoystick;
* layout now to immediately update the bounds. */
[data.uiwindow layoutIfNeeded];
}
+}
- sdlwindow = window;
+- (BOOL)shouldSwapDimensions:(BOOL)landscape
+{
+#if !TARGET_OS_TV
+ if (sdlwindow) {
+ SDL_VideoDisplay *display = SDL_GetDisplayForWindow(sdlwindow);
+ SDL_DisplayData *displaydata = (__bridge SDL_DisplayData *) display->driverdata;
+
+ if (displaydata.uiscreen == [UIScreen mainScreen]) {
+ NSUInteger orients = UIKit_GetSupportedOrientations(sdlwindow);
+ BOOL supportsLandscape = (orients & UIInterfaceOrientationMaskLandscape) != 0;
+ BOOL supportsPortrait = (orients & (UIInterfaceOrientationMaskPortrait|UIInterfaceOrientationMaskPortraitUpsideDown)) != 0;
+
+ /* Make sure the width/height are oriented correctly */
+ if ((landscape && !supportsLandscape) || (!landscape && !supportsPortrait)) {
+ return YES;
+ }
+ }
+ }
+#endif /* !TARGET_OS_TV */
+
+ return NO;
}
#if !TARGET_OS_TV && defined(__IPHONE_13_4)