Reference version

This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.

Expo SystemUI

一个允许与系统 UI 元素交互的库。

Android
iOS
tvOS
Web
Recommended version:
~57.0.0

expo-system-ui 使你能够与超出 React 树范围的 UI 元素进行交互。具体来说,包括根视图背景颜色,以及在 Android 上全局锁定用户界面样式。

安装

Terminal
npx expo install expo-system-ui

If you are installing this in an existing React Native app, make sure to install expo in your project.

应用配置中的配置

如果你在项目中使用配置插件(持续原生生成(CNG)),可以通过 expo-system-ui 内置的配置插件进行配置。该插件允许你从应用配置中配置 Android 上的 userInterfaceStyle 和 iOS 上的 backgroundColor 属性。这些属性不能在运行时设置,必须构建新的应用二进制文件后才会生效。如果你的应用使用 CNG,那么你需要手动配置该库。

Example app.json with config plugin

app.json
{ "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?

如果你没有使用持续原生生成(CNG),或者你是手动使用原生的 androidios 项目,那么你需要向原生项目添加以下配置:

Android

要在 Android 上应用 userInterfaceStyle,你需要在 android/app/src/main/res/values/strings.xml 中添加 expo_system_ui_user_interface_style 配置:

<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

SystemUI.getBackgroundColorAsync()

Gets the root view background color.

Returns:
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();

SystemUI.setBackgroundColorAsync(color)

ParameterTypeDescription
colorColorValue | null

Changes the root view background color. Call this function in the root file outside of your component.

Returns:
Promise<void>

Example

SystemUI.setBackgroundColorAsync("black");