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:
~58.0.1

expo-system-ui 可让你与 React 树之外的 UI 元素进行交互。具体来说,它可以设置根视图的背景颜色,并在 Android 上全局锁定用户界面样式。

安装

Terminal
- npx expo install expo-system-ui
- yarn expo install expo-system-ui
- pnpm expo install expo-system-ui
- bun 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),或者手动使用原生 android 和 ios 项目,则需要在原生项目中添加以下配置:

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> <!-- or dark --> </resources>

iOS

要在 iOS 上应用 backgroundColor,需要在 ios/your-app/Info.plist 中添加 UIUserInterfaceStyle 配置:

<plist> <dict> <!-- ... --> <key>UIUserInterfaceStyle</key> <string>Light</string> <!-- or Dark --> </dict> </plist>

API

import * as SystemUI from 'expo-system-ui';

Methods

SystemUI.getBackgroundColorAsync()

Android
iOS
tvOS
Web

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)

Android
iOS
tvOS
Web
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");