From 3dfedbc1aec18a4255e126fde96d5dc7b1271ea7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Kwas=CC=81niewski?= Date: Thu, 8 Feb 2024 02:39:35 -0800 Subject: [PATCH] (iOS) Add `RCTUIStatusBarManager` and properly retrieve StatusBar style and height (#42241) Summary: This PR migrates from the deprecated way of retrieving the status bar info. It introduces a helper method `RCTUIStatusBarManager` which gets the `UIStatusBarManager` from the KeyWindow. It also removes the unused `getHeight` method. ## Changelog: [IOS] [ADDED] - Add `RCTUIStatusBarManager` and properly retrieve StatusBar style and height [IOS] [REMOVED] - Remove unused getHeight method from StatusBar Pull Request resolved: https://github.com/facebook/react-native/pull/42241 Test Plan: CI Green, Ensure that preferredStatusBarStyle and preferredStatusBarHidden is properly retrieved for Modals Reviewed By: philIip Differential Revision: D52729974 Pulled By: cipolleschi fbshipit-source-id: 40adef810c1d419900fb7ba706af6fb095941e10 --- packages/react-native/React/Base/RCTUtils.h | 3 +++ packages/react-native/React/Base/RCTUtils.m | 5 +++++ .../React/CoreModules/RCTStatusBarManager.mm | 11 +++++++---- .../Modal/RCTFabricModalHostViewController.mm | 4 ++-- .../React/Views/RCTModalHostViewController.m | 4 ++-- 5 files changed, 19 insertions(+), 8 deletions(-) diff --git a/packages/react-native/React/Base/RCTUtils.h b/packages/react-native/React/Base/RCTUtils.h index 482a209aeea7f8..e03e412fa2ab51 100644 --- a/packages/react-native/React/Base/RCTUtils.h +++ b/packages/react-native/React/Base/RCTUtils.h @@ -99,6 +99,9 @@ RCT_EXTERN UIWindow *__nullable RCTKeyWindow(void); // e.g. to present a modal view controller or alert over it RCT_EXTERN UIViewController *__nullable RCTPresentedViewController(void); +// Retrieve current window UIStatusBarManager +RCT_EXTERN UIStatusBarManager *__nullable RCTUIStatusBarManager(void) API_AVAILABLE(ios(13)); + // Does this device support force touch (aka 3D Touch)? RCT_EXTERN BOOL RCTForceTouchAvailable(void); diff --git a/packages/react-native/React/Base/RCTUtils.m b/packages/react-native/React/Base/RCTUtils.m index 9626d90ab96d27..96e59e2db10181 100644 --- a/packages/react-native/React/Base/RCTUtils.m +++ b/packages/react-native/React/Base/RCTUtils.m @@ -579,6 +579,11 @@ BOOL RCTRunningInAppExtension(void) return nil; } +UIStatusBarManager *__nullable RCTUIStatusBarManager(void) +{ + return RCTKeyWindow().windowScene.statusBarManager; +} + UIViewController *__nullable RCTPresentedViewController(void) { if ([RCTUtilsUIOverride hasPresentedViewController]) { diff --git a/packages/react-native/React/CoreModules/RCTStatusBarManager.mm b/packages/react-native/React/CoreModules/RCTStatusBarManager.mm index 29309135309eac..015a91098e95cd 100644 --- a/packages/react-native/React/CoreModules/RCTStatusBarManager.mm +++ b/packages/react-native/React/CoreModules/RCTStatusBarManager.mm @@ -14,6 +14,9 @@ #import +static NSString *const kStatusBarFrameDidChange = @"statusBarFrameDidChange"; +static NSString *const kStatusBarFrameWillChange = @"statusBarFrameWillChange"; + @implementation RCTConvert (UIStatusBar) + (UIStatusBarStyle)UIStatusBarStyle:(id)json RCT_DYNAMIC @@ -71,7 +74,7 @@ + (BOOL)requiresMainQueueSetup - (NSArray *)supportedEvents { - return @[ @"statusBarFrameDidChange", @"statusBarFrameWillChange" ]; + return @[ kStatusBarFrameDidChange, kStatusBarFrameWillChange ]; } - (void)startObserving @@ -108,12 +111,12 @@ - (void)emitEvent:(NSString *)eventName forNotification:(NSNotification *)notifi - (void)applicationDidChangeStatusBarFrame:(NSNotification *)notification { - [self emitEvent:@"statusBarFrameDidChange" forNotification:notification]; + [self emitEvent:kStatusBarFrameDidChange forNotification:notification]; } - (void)applicationWillChangeStatusBarFrame:(NSNotification *)notification { - [self emitEvent:@"statusBarFrameWillChange" forNotification:notification]; + [self emitEvent:kStatusBarFrameWillChange forNotification:notification]; } RCT_EXPORT_METHOD(getHeight : (RCTResponseSenderBlock)callback) @@ -167,7 +170,7 @@ - (void)applicationWillChangeStatusBarFrame:(NSNotification *)notification __block facebook::react::ModuleConstants constants; RCTUnsafeExecuteOnMainQueueSync(^{ constants = facebook::react::typedConstants({ - .HEIGHT = RCTSharedApplication().statusBarFrame.size.height, + .HEIGHT = RCTUIStatusBarManager().statusBarFrame.size.height, .DEFAULT_BACKGROUND_COLOR = std::nullopt, }); }); diff --git a/packages/react-native/React/Fabric/Mounting/ComponentViews/Modal/RCTFabricModalHostViewController.mm b/packages/react-native/React/Fabric/Mounting/ComponentViews/Modal/RCTFabricModalHostViewController.mm index db5b92fb8f48e4..ff8810cba4001d 100644 --- a/packages/react-native/React/Fabric/Mounting/ComponentViews/Modal/RCTFabricModalHostViewController.mm +++ b/packages/react-native/React/Fabric/Mounting/ComponentViews/Modal/RCTFabricModalHostViewController.mm @@ -44,7 +44,7 @@ - (void)loadView - (UIStatusBarStyle)preferredStatusBarStyle { - return [RCTSharedApplication() statusBarStyle]; + return [RCTUIStatusBarManager() statusBarStyle]; } - (void)viewDidDisappear:(BOOL)animated @@ -55,7 +55,7 @@ - (void)viewDidDisappear:(BOOL)animated - (BOOL)prefersStatusBarHidden { - return [RCTSharedApplication() isStatusBarHidden]; + return [RCTUIStatusBarManager() isStatusBarHidden]; } #if RCT_DEV diff --git a/packages/react-native/React/Views/RCTModalHostViewController.m b/packages/react-native/React/Views/RCTModalHostViewController.m index 059b64157f9836..50075ffd4847c3 100644 --- a/packages/react-native/React/Views/RCTModalHostViewController.m +++ b/packages/react-native/React/Views/RCTModalHostViewController.m @@ -24,8 +24,8 @@ - (instancetype)init self.modalInPresentation = YES; - _preferredStatusBarStyle = [RCTSharedApplication() statusBarStyle]; - _preferredStatusBarHidden = [RCTSharedApplication() isStatusBarHidden]; + _preferredStatusBarStyle = [RCTUIStatusBarManager() statusBarStyle]; + _preferredStatusBarHidden = [RCTUIStatusBarManager() isStatusBarHidden]; return self; }