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: https://github.com/facebook/react-native/issues/7340
Closes https://github.com/facebook/react-native/pull/11350

Differential Revision: D4348186

Pulled By: javache

fbshipit-source-id: cb2cfb9cccfc459ad4b46a5af2bec4c973132ae8
This commit is contained in:
Mani Ghasemlou 2016-12-19 14:51:14 -08:00 committed by Facebook Github Bot
parent ac11eedfb0
commit a19c6991c0
1 changed files with 9 additions and 9 deletions

View File

@ -246,11 +246,11 @@ RCT_EXPORT_MODULE()
}); });
} }
- (void)interfaceOrientationWillChange:(NSNotification *)notification - (void)interfaceOrientationDidChange
{ {
#if !TARGET_OS_TV #if !TARGET_OS_TV
UIInterfaceOrientation nextOrientation = UIInterfaceOrientation nextOrientation =
[notification.userInfo[UIApplicationStatusBarOrientationUserInfoKey] integerValue]; [RCTSharedApplication() statusBarOrientation];
// Update when we go from portrait to landscape, or landscape to portrait // Update when we go from portrait to landscape, or landscape to portrait
if ((UIInterfaceOrientationIsPortrait(_currentInterfaceOrientation) && if ((UIInterfaceOrientationIsPortrait(_currentInterfaceOrientation) &&
@ -260,7 +260,7 @@ RCT_EXPORT_MODULE()
#pragma clang diagnostic push #pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations" #pragma clang diagnostic ignored "-Wdeprecated-declarations"
[_bridge.eventDispatcher sendDeviceEventWithName:@"didUpdateDimensions" [_bridge.eventDispatcher sendDeviceEventWithName:@"didUpdateDimensions"
body:RCTExportedDimensions(YES)]; body:RCTExportedDimensions()];
#pragma clang diagnostic pop #pragma clang diagnostic pop
} }
@ -347,8 +347,8 @@ RCT_EXPORT_MODULE()
object:_bridge.accessibilityManager]; object:_bridge.accessibilityManager];
#if !TARGET_OS_TV #if !TARGET_OS_TV
[[NSNotificationCenter defaultCenter] addObserver:self [[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(interfaceOrientationWillChange:) selector:@selector(interfaceOrientationDidChange)
name:UIApplicationWillChangeStatusBarOrientationNotification name:UIApplicationDidChangeStatusBarOrientationNotification
object:nil]; object:nil];
#endif #endif
@ -1525,12 +1525,12 @@ RCT_EXPORT_METHOD(clearJSResponder)
constants[@"customBubblingEventTypes"] = bubblingEvents; constants[@"customBubblingEventTypes"] = bubblingEvents;
constants[@"customDirectEventTypes"] = directEvents; constants[@"customDirectEventTypes"] = directEvents;
constants[@"Dimensions"] = RCTExportedDimensions(NO); constants[@"Dimensions"] = RCTExportedDimensions();
return constants; return constants;
} }
static NSDictionary *RCTExportedDimensions(BOOL rotateBounds) static NSDictionary *RCTExportedDimensions()
{ {
RCTAssertMainQueue(); RCTAssertMainQueue();
@ -1538,8 +1538,8 @@ static NSDictionary *RCTExportedDimensions(BOOL rotateBounds)
CGRect screenSize = [[UIScreen mainScreen] bounds]; CGRect screenSize = [[UIScreen mainScreen] bounds];
return @{ return @{
@"window": @{ @"window": @{
@"width": @(rotateBounds ? screenSize.size.height : screenSize.size.width), @"width": @(screenSize.size.width),
@"height": @(rotateBounds ? screenSize.size.width : screenSize.size.height), @"height": @(screenSize.size.height),
@"scale": @(RCTScreenScale()), @"scale": @(RCTScreenScale()),
}, },
}; };