Permalink
Browse files

Fix for dimensions not updating correctly on iPad due to screen rotation

Summary:
On certain devices (in my case, any iPad Pro model), listening to `DeviceEventEmitter.didUpdateDimensions` would call back *before* the interface change takes places (i.e. calling `Dimensions.get()` in this callback would return wrong values). Turns out that we were listening for the wrong native event.

Edit to add: now using `[RCTSharedApplication() statusBarOrientation]` to get the current orientation. Not yet sure why, but the `userInfo` provided by the notification was returning the wrong orientation *only* on the first time you rotate the device.

This fixes the open issue: #7340
Closes #11350

Differential Revision: D4348186

Pulled By: javache

fbshipit-source-id: cb2cfb9cccfc459ad4b46a5af2bec4c973132ae8
  • Loading branch information...
Mani Ghasemlou authored and facebook-github-bot committed Dec 19, 2016
1 parent ac11eed commit a19c6991c06fd8606f4e6ae6e52b611583f342db
Showing with 9 additions and 9 deletions.
  1. +9 −9 React/Modules/RCTUIManager.m
@@ -246,11 +246,11 @@ - (void)didReceiveNewContentSizeMultiplier
});
}
- (void)interfaceOrientationWillChange:(NSNotification *)notification
- (void)interfaceOrientationDidChange
{
#if !TARGET_OS_TV
UIInterfaceOrientation nextOrientation =
[notification.userInfo[UIApplicationStatusBarOrientationUserInfoKey] integerValue];
[RCTSharedApplication() statusBarOrientation];
// Update when we go from portrait to landscape, or landscape to portrait
if ((UIInterfaceOrientationIsPortrait(_currentInterfaceOrientation) &&
@@ -260,7 +260,7 @@ - (void)interfaceOrientationWillChange:(NSNotification *)notification
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
[_bridge.eventDispatcher sendDeviceEventWithName:@"didUpdateDimensions"
body:RCTExportedDimensions(YES)];
body:RCTExportedDimensions()];
#pragma clang diagnostic pop
}
@@ -347,8 +347,8 @@ - (void)setBridge:(RCTBridge *)bridge
object:_bridge.accessibilityManager];
#if !TARGET_OS_TV
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(interfaceOrientationWillChange:)
name:UIApplicationWillChangeStatusBarOrientationNotification
selector:@selector(interfaceOrientationDidChange)
name:UIApplicationDidChangeStatusBarOrientationNotification
object:nil];
#endif
@@ -1525,21 +1525,21 @@ static void RCTMeasureLayout(RCTShadowView *view,
constants[@"customBubblingEventTypes"] = bubblingEvents;
constants[@"customDirectEventTypes"] = directEvents;
constants[@"Dimensions"] = RCTExportedDimensions(NO);
constants[@"Dimensions"] = RCTExportedDimensions();
return constants;
}
static NSDictionary *RCTExportedDimensions(BOOL rotateBounds)
static NSDictionary *RCTExportedDimensions()
{
RCTAssertMainQueue();
// Don't use RCTScreenSize since it the interface orientation doesn't apply to it
CGRect screenSize = [[UIScreen mainScreen] bounds];
return @{
@"window": @{
@"width": @(rotateBounds ? screenSize.size.height : screenSize.size.width),
@"height": @(rotateBounds ? screenSize.size.width : screenSize.size.height),
@"width": @(screenSize.size.width),
@"height": @(screenSize.size.height),
@"scale": @(RCTScreenScale()),
},
};

0 comments on commit a19c699

Please sign in to comment.