mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Fix Application always in light mode on initial load. (#44335)
Summary: Hi, I'm Filip from software mansion. This PR solves a problem I stumbled upon. On iOS, applications are always in light mode on initial load. Even if the device is turned to dark mode. ### Cause of the problem: The initial appearance is taken from `RCTKeyWindow()`, but at the time of initialization of `RCTAppearance` it does not exist yet. ### Solution: This PR moves repeats initialization of the appearance the first time `getColorScheme()` is called if it was not initialized properly before. ## Changelog: [IOS] [FIXED] - Fix dark mode on initial load. Pull Request resolved: https://github.com/facebook/react-native/pull/44335 Test Plan: - Create new React native app with `npx react-native@latest init AwesomeProjec` - Run the application on iphone using simulator - turn on dark mode using `cmd+shift+A` - close application and run it again ### without changes: The application will turn on in light mode despite the simulator being set to dark mode. When you reload the application it works as expected (is in dark mode) ### with changes: Works as expected #### note: any change to device ui settings will trigger a listener that will set appearance to correct state, so testing of this problem should happen in as isolated conditions as possible. Reviewed By: cortinico Differential Revision: D58189058 Pulled By: cipolleschi fbshipit-source-id: 9a864f3d045e966bc88601f661d221c4796c5c95
This commit is contained in:
committed by
Facebook GitHub Bot
parent
b5fd041917
commit
b957513cc6
@@ -19,6 +19,8 @@ using namespace facebook::react;
|
||||
NSString *const RCTAppearanceColorSchemeLight = @"light";
|
||||
NSString *const RCTAppearanceColorSchemeDark = @"dark";
|
||||
|
||||
static BOOL sIsAppearancePreferenceSet = NO;
|
||||
|
||||
static BOOL sAppearancePreferenceEnabled = YES;
|
||||
void RCTEnableAppearancePreference(BOOL enabled)
|
||||
{
|
||||
@@ -62,6 +64,12 @@ NSString *RCTColorSchemePreference(UITraitCollection *traitCollection)
|
||||
// Return the default if the app doesn't allow different color schemes.
|
||||
return RCTAppearanceColorSchemeLight;
|
||||
}
|
||||
|
||||
if (appearances[@(traitCollection.userInterfaceStyle)]) {
|
||||
sIsAppearancePreferenceSet = YES;
|
||||
return appearances[@(traitCollection.userInterfaceStyle)];
|
||||
}
|
||||
|
||||
UIUserInterfaceStyle systemStyle = sUseKeyWindowForSystemStyle ? RCTKeyWindow().traitCollection.userInterfaceStyle
|
||||
: traitCollection.userInterfaceStyle;
|
||||
return appearances[@(systemStyle)] ?: RCTAppearanceColorSchemeLight;
|
||||
@@ -116,6 +124,10 @@ RCT_EXPORT_METHOD(setColorScheme : (NSString *)style)
|
||||
|
||||
RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSString *, getColorScheme)
|
||||
{
|
||||
if (!sIsAppearancePreferenceSet) {
|
||||
UITraitCollection *traitCollection = RCTKeyWindow().traitCollection;
|
||||
_currentColorScheme = RCTColorSchemePreference(traitCollection);
|
||||
}
|
||||
return _currentColorScheme;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user