From 8901a4d8f5de3b52904d39f24c3c047b08134277 Mon Sep 17 00:00:00 2001 From: Miguel Gaeta Date: Tue, 10 Sep 2024 17:20:05 -0700 Subject: [PATCH] [IOS] Fix a bug in reported device window/screen size for iPad and possibly iPhone. --- .../React/CoreModules/RCTDeviceInfo.mm | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/packages/react-native/React/CoreModules/RCTDeviceInfo.mm b/packages/react-native/React/CoreModules/RCTDeviceInfo.mm index 454a3252cc5e1f..ad0bfd09532982 100644 --- a/packages/react-native/React/CoreModules/RCTDeviceInfo.mm +++ b/packages/react-native/React/CoreModules/RCTDeviceInfo.mm @@ -74,7 +74,7 @@ - (void)initialize _currentInterfaceOrientation = RCTKeyWindow().windowScene.interfaceOrientation; [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(interfaceFrameDidChange) + selector:@selector(interfaceFrameDidChangeAsync) name:UIDeviceOrientationDidChangeNotification object:nil]; #endif @@ -253,6 +253,23 @@ - (void)interfaceFrameDidChange RCTExecuteOnMainQueue(^{ [weakSelf _interfaceFrameDidChange]; }); + +} + +// Because this RCTDeviceInfo uses dispatch_get_main_queue, RCTExecuteOnMainQueue +// as specified in the interfaceFrameDidChange method will run without delay. +// +// The call to get window measurements may not be accurate as the window +// may not have updated yet. To ensure we get the correct window measurements +// use `dispatch_async` to delay a tick and wait for the window to update. +// +// This bug was observed and reproduced on an iPad but may exist for iPhone as well. +- (void)interfaceFrameDidChangeAsync +{ + __weak __typeof(self) weakSelf = self; + dispatch_async(dispatch_get_main_queue(), ^{ + [weakSelf _interfaceFrameDidChange]; + }); } - (void)_interfaceFrameDidChange