From a19c6991c06fd8606f4e6ae6e52b611583f342db Mon Sep 17 00:00:00 2001 From: Mani Ghasemlou Date: Mon, 19 Dec 2016 14:51:14 -0800 Subject: [PATCH] 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 --- React/Modules/RCTUIManager.m | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/React/Modules/RCTUIManager.m b/React/Modules/RCTUIManager.m index d52a6fda1..58ffe2985 100644 --- a/React/Modules/RCTUIManager.m +++ b/React/Modules/RCTUIManager.m @@ -246,11 +246,11 @@ RCT_EXPORT_MODULE() }); } -- (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 @@ RCT_EXPORT_MODULE() #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 @@ RCT_EXPORT_MODULE() 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,12 +1525,12 @@ RCT_EXPORT_METHOD(clearJSResponder) constants[@"customBubblingEventTypes"] = bubblingEvents; constants[@"customDirectEventTypes"] = directEvents; - constants[@"Dimensions"] = RCTExportedDimensions(NO); + constants[@"Dimensions"] = RCTExportedDimensions(); return constants; } -static NSDictionary *RCTExportedDimensions(BOOL rotateBounds) +static NSDictionary *RCTExportedDimensions() { RCTAssertMainQueue(); @@ -1538,8 +1538,8 @@ static NSDictionary *RCTExportedDimensions(BOOL rotateBounds) 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()), }, };