Skip to content

Commit

Permalink
[IOS] Fix a bug in reported device window/screen size for iPad and po…
Browse files Browse the repository at this point in the history
…ssibly iPhone.
  • Loading branch information
mrkcsc authored and joemun committed Nov 6, 2024
1 parent 891f4e9 commit 4af3076
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion packages/react-native/React/CoreModules/RCTDeviceInfo.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4af3076

Please sign in to comment.