(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
This commit is contained in:
Oskar Kwaśniewski
2024-02-08 02:39:35 -08:00
committed by Facebook GitHub Bot
parent c7a0dff760
commit 3dfedbc1ae
5 changed files with 19 additions and 8 deletions
@@ -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);
@@ -579,6 +579,11 @@ UIWindow *__nullable RCTKeyWindow(void)
return nil;
}
UIStatusBarManager *__nullable RCTUIStatusBarManager(void)
{
return RCTKeyWindow().windowScene.statusBarManager;
}
UIViewController *__nullable RCTPresentedViewController(void)
{
if ([RCTUtilsUIOverride hasPresentedViewController]) {
@@ -14,6 +14,9 @@
#import <FBReactNativeSpec/FBReactNativeSpec.h>
static NSString *const kStatusBarFrameDidChange = @"statusBarFrameDidChange";
static NSString *const kStatusBarFrameWillChange = @"statusBarFrameWillChange";
@implementation RCTConvert (UIStatusBar)
+ (UIStatusBarStyle)UIStatusBarStyle:(id)json RCT_DYNAMIC
@@ -71,7 +74,7 @@ RCT_EXPORT_MODULE()
- (NSArray<NSString *> *)supportedEvents
{
return @[ @"statusBarFrameDidChange", @"statusBarFrameWillChange" ];
return @[ kStatusBarFrameDidChange, kStatusBarFrameWillChange ];
}
- (void)startObserving
@@ -108,12 +111,12 @@ RCT_EXPORT_MODULE()
- (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 @@ RCT_EXPORT_METHOD(setNetworkActivityIndicatorVisible : (BOOL)visible)
__block facebook::react::ModuleConstants<JS::NativeStatusBarManagerIOS::Constants> constants;
RCTUnsafeExecuteOnMainQueueSync(^{
constants = facebook::react::typedConstants<JS::NativeStatusBarManagerIOS::Constants>({
.HEIGHT = RCTSharedApplication().statusBarFrame.size.height,
.HEIGHT = RCTUIStatusBarManager().statusBarFrame.size.height,
.DEFAULT_BACKGROUND_COLOR = std::nullopt,
});
});
@@ -44,7 +44,7 @@
- (UIStatusBarStyle)preferredStatusBarStyle
{
return [RCTSharedApplication() statusBarStyle];
return [RCTUIStatusBarManager() statusBarStyle];
}
- (void)viewDidDisappear:(BOOL)animated
@@ -55,7 +55,7 @@
- (BOOL)prefersStatusBarHidden
{
return [RCTSharedApplication() isStatusBarHidden];
return [RCTUIStatusBarManager() isStatusBarHidden];
}
#if RCT_DEV
@@ -24,8 +24,8 @@
self.modalInPresentation = YES;
_preferredStatusBarStyle = [RCTSharedApplication() statusBarStyle];
_preferredStatusBarHidden = [RCTSharedApplication() isStatusBarHidden];
_preferredStatusBarStyle = [RCTUIStatusBarManager() statusBarStyle];
_preferredStatusBarHidden = [RCTUIStatusBarManager() isStatusBarHidden];
return self;
}