Expo SystemUI
一个允许与系统 UI 元素交互的库。
For the complete documentation index, see llms.txt. Use this Use this file to discover all available pages.
expo-system-ui 使你能够与 React 树之外的 UI 元素交互。具体来说,包括根视图的背景颜色,以及在 Android 上全局锁定用户界面样式。
安装
- npx expo install expo-system-uiIf you are installing this in an existing React Native app, make sure to install expo in your project.
app config 中的配置
当你使用 Continuous Native Generation (CNG) 时,启用 expo-system-ui 配置插件。该插件允许你从 app config 中配置 Android 上的 userInterfaceStyle 和 iOS 上的 backgroundColor 属性。这些属性无法在运行时设置,需要构建新的应用二进制文件后才会生效。如果你的应用不使用 CNG,那么你需要手动配置此库。
Example app.json with config plugin
{ "expo": { "backgroundColor": "#ffffff", "userInterfaceStyle": "light", "ios": { "backgroundColor": "#ffffff", } "android": { "userInterfaceStyle": "light" }, "plugins": ["expo-system-ui"], } }
Are you using this library in an existing React Native app?
如果你没有使用 Continuous Native Generation (CNG),或者你正在手动使用原生 android 和 ios 项目,那么你需要将以下配置添加到你的原生项目中:
Android
要在 Android 上应用 userInterfaceStyle,你需要将 expo_system_ui_user_interface_style 配置添加到 android/app/src/main/res/values/strings.xml:
<resources> <!-- ... --> <string name="expo_system_ui_user_interface_style" translatable="false">light</string> <!-- 或 dark --> </resources>
iOS
要在 iOS 上应用 backgroundColor,你需要在 ios/your-app/Info.plist 中添加 UIUserInterfaceStyle 配置:
<plist> <dict> <!-- ... --> <key>UIUserInterfaceStyle</key> <string>Light</string> <!-- 或 Dark --> </dict> </plist>
API
import * as SystemUI from 'expo-system-ui';
Methods
Gets the root view background color.
Promise<ColorValue | null>Current root view background color in hex format. Returns null if the background color is not set.
Example
const color = await SystemUI.getBackgroundColorAsync();
| Parameter | Type | Description |
|---|---|---|
| color | null | ColorValue | Any valid CSS 3 (SVG) color. |
Changes the root view background color. Call this function in the root file outside of your component.
Promise<void>Example
SystemUI.setBackgroundColorAsync("black");