Skip to content

Commit

Permalink
(iOS) Add RCTUIStatusBarManager and properly retrieve StatusBar sty…
Browse files Browse the repository at this point in the history
…le 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: #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
  • Loading branch information
okwasniewski authored and facebook-github-bot committed Feb 8, 2024
1 parent c7a0dff commit 3dfedbc
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 8 deletions.
3 changes: 3 additions & 0 deletions packages/react-native/React/Base/RCTUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
5 changes: 5 additions & 0 deletions packages/react-native/React/Base/RCTUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,11 @@ BOOL RCTRunningInAppExtension(void)
return nil;
}

UIStatusBarManager *__nullable RCTUIStatusBarManager(void)
{
return RCTKeyWindow().windowScene.statusBarManager;
}

UIViewController *__nullable RCTPresentedViewController(void)
{
if ([RCTUtilsUIOverride hasPresentedViewController]) {
Expand Down
11 changes: 7 additions & 4 deletions packages/react-native/React/CoreModules/RCTStatusBarManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -71,7 +74,7 @@ + (BOOL)requiresMainQueueSetup

- (NSArray<NSString *> *)supportedEvents
{
return @[ @"statusBarFrameDidChange", @"statusBarFrameWillChange" ];
return @[ kStatusBarFrameDidChange, kStatusBarFrameWillChange ];
}

- (void)startObserving
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -167,7 +170,7 @@ - (void)applicationWillChangeStatusBarFrame:(NSNotification *)notification
__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,
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ - (void)loadView

- (UIStatusBarStyle)preferredStatusBarStyle
{
return [RCTSharedApplication() statusBarStyle];
return [RCTUIStatusBarManager() statusBarStyle];
}

- (void)viewDidDisappear:(BOOL)animated
Expand All @@ -55,7 +55,7 @@ - (void)viewDidDisappear:(BOOL)animated

- (BOOL)prefersStatusBarHidden
{
return [RCTSharedApplication() isStatusBarHidden];
return [RCTUIStatusBarManager() isStatusBarHidden];
}

#if RCT_DEV
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ - (instancetype)init

self.modalInPresentation = YES;

_preferredStatusBarStyle = [RCTSharedApplication() statusBarStyle];
_preferredStatusBarHidden = [RCTSharedApplication() isStatusBarHidden];
_preferredStatusBarStyle = [RCTUIStatusBarManager() statusBarStyle];
_preferredStatusBarHidden = [RCTUIStatusBarManager() isStatusBarHidden];

return self;
}
Expand Down

0 comments on commit 3dfedbc

Please sign in to comment.