Skip to content

Commit

Permalink
fix: RCTAlertController's UserInterfaceStyle to follow root window (#…
Browse files Browse the repository at this point in the history
…34218)

Summary:
The motivation of this PR is for the Alert to follow the same style override (`overrideUserInterfaceStyle` being light/dark) as the one used by the root window (`UIApplication.sharedApplication.delegate.window`).

This is something that has worked previously because `RCTPResentedViewController()` was used to present the Alert (the behavior has changed in vonovak@f319ff3). With the former approach, the alert would "inherit" the `userInterfaceStyle` of the view controller it was presented within (and that one, in turn, would "inherit" from `UIApplication.sharedApplication.delegate.window`).

With the current approach, the "style inheritance" does not work with the view controller being created [here](https://github.com/facebook/react-native/blob/f3db6cc52792e3006a16408df4ae40f3aee19a86/React/CoreModules/RCTAlertController.m#L24).

Because this viewcontroller instance does not have where to "inherit" the styling from, the styling might be different from the rest of the app. This PR fixes that.

## Changelog

[iOS] [Fixed] - fix: RCTAlertController's UserInterfaceStyle to follow root window

Pull Request resolved: #34218

Test Plan:
Instead of

```
self.overrideUserInterfaceStyle = UIApplication.sharedApplication.delegate.window.overrideUserInterfaceStyle;
```

you can do

```
self.overrideUserInterfaceStyle = UIUserInterfaceStyleDark;
```

and observe the result. So if the override is set, it'll manifest itself. If it's not set, the value of `UIApplication.sharedApplication.delegate.window.overrideUserInterfaceStyle` will be `UIUserInterfaceStyleUnspecified`, and it'll have no effect.

<details>
  <summary>screenshot</summary>

![Simulator Screen Shot - iPhone 11 - 2022-07-18 at 21 40 06](https://user-images.githubusercontent.com/1566403/179616673-d0e48e07-50b5-41a1-afb7-0aa8f7ec37b5.png)
</details>

Reviewed By: dmitryrykun

Differential Revision: D38660799

Pulled By: cipolleschi

fbshipit-source-id: c979266900e27be7a4732bdb6e9a496906534931
  • Loading branch information
vonovak authored and facebook-github-bot committed Aug 25, 2022
1 parent be7c50f commit 18542b6
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions React/CoreModules/RCTAlertController.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ - (UIWindow *)alertWindow

- (void)show:(BOOL)animated completion:(void (^)(void))completion
{
if (@available(iOS 13.0, *)) {
UIUserInterfaceStyle style =
RCTSharedApplication().delegate.window.overrideUserInterfaceStyle ?: UIUserInterfaceStyleUnspecified;
self.overrideUserInterfaceStyle = style;
}
[self.alertWindow makeKeyAndVisible];
[self.alertWindow.rootViewController presentViewController:self animated:animated completion:completion];
}
Expand Down

0 comments on commit 18542b6

Please sign in to comment.