From 925c53afcfb65c6ab5d7736715fc95af2db7e38b Mon Sep 17 00:00:00 2001 From: luoxuhai <15186743693@163.com> Date: Sat, 2 Apr 2022 09:37:37 +0800 Subject: [PATCH] Added userInterfaceStyle to Alert to override user interface style for iOS 13+ --- Libraries/Alert/Alert.js | 13 ++++++++++++- Libraries/Alert/NativeAlertManager.js | 1 + React/CoreModules/RCTAlertManager.mm | 23 +++++++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/Libraries/Alert/Alert.js b/Libraries/Alert/Alert.js index 9bd5484d8c3e..5dbe5cb09efb 100644 --- a/Libraries/Alert/Alert.js +++ b/Libraries/Alert/Alert.js @@ -27,6 +27,7 @@ export type Buttons = Array<{ type Options = { cancelable?: ?boolean, + userInterfaceStyle?: 'unspecified' | 'light' | 'dark', onDismiss?: ?() => void, ... }; @@ -44,7 +45,15 @@ class Alert { options?: Options, ): void { if (Platform.OS === 'ios') { - Alert.prompt(title, message, buttons, 'default'); + Alert.prompt( + title, + message, + buttons, + 'default', + undefined, + undefined, + options, + ); } else if (Platform.OS === 'android') { const NativeDialogManagerAndroid = require('../NativeModules/specs/NativeDialogManagerAndroid').default; @@ -107,6 +116,7 @@ class Alert { type?: ?AlertType = 'plain-text', defaultValue?: string, keyboardType?: string, + options?: Options, ): void { if (Platform.OS === 'ios') { let callbacks = []; @@ -141,6 +151,7 @@ class Alert { cancelButtonKey, destructiveButtonKey, keyboardType, + userInterfaceStyle: options?.userInterfaceStyle || undefined, }, (id, value) => { const cb = callbacks[id]; diff --git a/Libraries/Alert/NativeAlertManager.js b/Libraries/Alert/NativeAlertManager.js index 899455bda948..9597ef155a52 100644 --- a/Libraries/Alert/NativeAlertManager.js +++ b/Libraries/Alert/NativeAlertManager.js @@ -20,6 +20,7 @@ export type Args = {| cancelButtonKey?: string, destructiveButtonKey?: string, keyboardType?: string, + userInterfaceStyle?: string, |}; export interface Spec extends TurboModule { diff --git a/React/CoreModules/RCTAlertManager.mm b/React/CoreModules/RCTAlertManager.mm index c4a1cde52dcb..5a66e57fb8fc 100644 --- a/React/CoreModules/RCTAlertManager.mm +++ b/React/CoreModules/RCTAlertManager.mm @@ -32,6 +32,20 @@ @implementation RCTConvert (UIAlertViewStyle) @end +@implementation RCTConvert (UIUserInterfaceStyle) + +RCT_ENUM_CONVERTER( + UIUserInterfaceStyle, + (@{ + @"unspecified" : @(UIUserInterfaceStyleUnspecified), + @"light" : @(UIUserInterfaceStyleLight), + @"dark" : @(UIUserInterfaceStyleDark), + }), + UIUserInterfaceStyleUnspecified, + integerValue) + +@end + @interface RCTAlertManager () @end @@ -103,6 +117,15 @@ - (void)invalidate RCTAlertController *alertController = [RCTAlertController alertControllerWithTitle:title message:nil preferredStyle:UIAlertControllerStyleAlert]; + +#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && defined(__IPHONE_13_0) && \ + __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0 + if (@available(iOS 13.0, *)) { + UIUserInterfaceStyle userInterfaceStyle = [RCTConvert UIUserInterfaceStyle:args.userInterfaceStyle()]; + alertController.overrideUserInterfaceStyle = userInterfaceStyle; + } +#endif + switch (type) { case RCTAlertViewStylePlainTextInput: { [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {