-
Notifications
You must be signed in to change notification settings - Fork 24.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UIViewController-based status bar management (#25919)
Summary: {emoji:26a0} This is a follow up to #25425 -- which isn't merged yet… See https://github.com/facebook/react-native/pull/25919/files/2a286257a6553a80a34e2b1f1ad94fc7bae36ea3..125aedbedc234c65c8d1b2133b79e926ad6cf145 for actual diff Currently, StatusBar native module manages the status bar on iOS globally, using `UIApplication.` APIs. This is bad because: - those APIs have been deprecated for 4 years - Apple really, really wants you to have an explicitly defined view controller, and control the status bar there - it [breaks external native components](#25181 (comment)) - it's [not compatible with iPadOS 13 multi window support](#25181 (comment)) for those reasons I we should transition towards view controller-based status bar management. With that, there is a need to introduce a default React Native root view controller, so I added `RCTRootViewController`. Using it is completely opt-in and there is no breaking change here. However I believe this should be a part of the template for new RN iOS apps. Additionally, I added `RCTRootViewControllerProtocol` with hooks needed for RCTStatusBarManager to control the status bar. This means apps that want to have total control over their view controller can still opt in to react native VC-based status bar by conforming their root view controller to this protocol. ## Changelog [iOS] [Added] - Added `RCTRootViewController` and `RCTRootViewControllerProtocol` [iOS] [Fixed] - `UIViewControllerBasedStatusBarAppearance=YES` no longer triggers an error as long as you use `RCTRootViewController` [iOS] [Fixed] - Status bar style is now correctly changed in multi-window iPadOS 13 apps if you use `RCTRootViewController` and set `UIViewControllerBasedStatusBarAppearance=YES` Pull Request resolved: #25919 Test Plan: - Open RNTester → StatusBar → and check that no features broke Reviewed By: fkgozali Differential Revision: D16957766 Pulled By: hramos fbshipit-source-id: 9ae1384ee20a06933053c4404b8237810f1e7c2c
- Loading branch information
1 parent
b58e176
commit 80e6d67
Showing
7 changed files
with
177 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
#import <UIKit/UIKit.h> | ||
|
||
#import <React/RCTBridge.h> | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
@class RCTRootView; | ||
|
||
@protocol RCTRootViewControllerProtocol <NSObject> | ||
|
||
/** | ||
* RCTStatusBarManager calls this to update the status bar style. | ||
* | ||
* Conforming view controllers should use this to update preferred status bar style | ||
*/ | ||
- (void)updateStatusBarStyle:(UIStatusBarStyle)style | ||
hidden:(BOOL)hidden | ||
animation:(UIStatusBarAnimation)animation | ||
animated:(BOOL)animate; | ||
|
||
@end | ||
|
||
@interface RCTRootViewController : UIViewController <RCTRootViewControllerProtocol> | ||
|
||
/** | ||
* - Designated initializer - | ||
*/ | ||
- (instancetype)initWithRootView:(RCTRootView *)rootView NS_DESIGNATED_INITIALIZER; | ||
|
||
/** | ||
* The root view used by the view controller. | ||
*/ | ||
@property (nonatomic, strong, readonly) RCTRootView *rootView; | ||
|
||
/** | ||
* See: RCTRootViewControllerProtocol | ||
*/ | ||
- (void)updateStatusBarStyle:(UIStatusBarStyle)style | ||
hidden:(BOOL)hidden | ||
animation:(UIStatusBarAnimation)animation | ||
animated:(BOOL)animate; | ||
|
||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
#import "RCTRootViewController.h" | ||
#import "RCTUtils.h" | ||
#import "RCTRootView.h" | ||
|
||
@implementation RCTRootViewController | ||
{ | ||
UIStatusBarStyle _statusBarStyle; | ||
BOOL _statusBarHidden; | ||
UIStatusBarAnimation _statusBarAnimation; | ||
} | ||
|
||
- (instancetype)initWithRootView:(RCTRootView *)rootView | ||
{ | ||
RCTAssertParam(rootView); | ||
|
||
if (self = [super initWithNibName:nil bundle:nil]) { | ||
_rootView = rootView; | ||
_statusBarStyle = UIStatusBarStyleDefault; | ||
_statusBarHidden = false; | ||
_statusBarAnimation = UIStatusBarAnimationFade; | ||
} | ||
|
||
return self; | ||
} | ||
|
||
RCT_NOT_IMPLEMENTED(- (instancetype)initWithNibName:(NSString *)nn bundle:(NSBundle *)nb) | ||
RCT_NOT_IMPLEMENTED(- (instancetype)initWithCoder:(NSCoder *)aDecoder) | ||
|
||
- (void)loadView | ||
{ | ||
self.view = _rootView; | ||
} | ||
|
||
- (UIStatusBarStyle)preferredStatusBarStyle | ||
{ | ||
return _statusBarStyle; | ||
} | ||
|
||
- (BOOL)prefersStatusBarHidden | ||
{ | ||
return _statusBarHidden; | ||
} | ||
|
||
- (UIStatusBarAnimation)preferredStatusBarUpdateAnimation | ||
{ | ||
return _statusBarAnimation; | ||
} | ||
|
||
- (void)updateStatusBarStyle:(UIStatusBarStyle)style | ||
hidden:(BOOL)hidden | ||
animation:(UIStatusBarAnimation)animation | ||
animated:(BOOL)animate; | ||
This comment has been minimized.
Sorry, something went wrong. |
||
{ | ||
_statusBarStyle = style; | ||
_statusBarHidden = hidden; | ||
_statusBarAnimation = animation; | ||
if (animate) { | ||
[UIView animateWithDuration:0.150 animations:^{ | ||
[self setNeedsStatusBarAppearanceUpdate]; | ||
}]; | ||
} else { | ||
[self setNeedsStatusBarAppearanceUpdate]; | ||
} | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The semicolon seems redundant