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 状态栏 iconExpo 状态栏

一个提供与 React Native StatusBar API 相同接口的库,但默认值略有不同,以便在 Expo 环境中更好地工作。

Android
iOS
tvOS
Web
Included in Expo Go
Recommended version:
~57.0.0

expo-status-bar 为你提供了一个组件和命令式接口,用于控制应用状态栏,以更改其文字颜色、隐藏它,并为这些更改应用动画。你能够通过 StatusBar 组件完成的具体操作取决于你所使用的平台。

安装

Terminal
npx expo install expo-status-bar

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

app 配置中的配置

如果你在项目中使用配置插件(Continuous Native Generation (CNG)),可以使用 expo-status-bar 自带的 config plugin 进行配置。该插件允许你配置各种无法在运行时设置、且需要重新构建新的应用二进制文件后才会生效的属性。如果你的应用使用 CNG,那么你需要手动配置该库。

Example app.json with config plugin

app.json
{ "expo": { "plugins": [ [ "expo-status-bar", { "hidden": false, "style": "dark" } ] ] } }

Configurable properties

NameDefaultDescription
hiddenundefined

决定状态栏是否在启动时隐藏。可接受 truefalse 作为值。

styleundefined

决定状态栏启动时采用哪种样式。可接受 lightdark 作为值。

Are you using this library in an existing React Native app?

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

  • 要在 Android 上隐藏状态栏,请将 expoStatusBarHidden 添加到 android/app/src/main/res/values/styles.xml

    <style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar"> <!-- ... --> <item name="expoStatusBarHidden">true</item> </style>
  • 要在 iOS 上隐藏状态栏,请在你的 ios/<project>/Info.plist 中设置以下键:

    <key>UIStatusBarHidden</key> <true/>

用法

Example
import { StyleSheet, Text, View } from 'react-native'; import { StatusBar } from 'expo-status-bar'; export default function App() { return ( <View style={styles.container}> <Text style={styles.text}>注意状态栏使用的是浅色文字!</Text> <StatusBar style="light" /> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#000', alignItems: 'center', justifyContent: 'center', }, text: { color: '#fff', }, });

API

import { StatusBar } from 'expo-status-bar';

Component

StatusBar

Type: React.Element<StatusBarProps>

A component that allows you to configure your status bar declaratively.

You will likely have multiple StatusBar components mounted in the same app at the same time. For example, if you have multiple screens in your app, you may end up using one per screen. The props of each StatusBar component will be merged in the order that they were mounted. This component is built on top of the StatusBar component exported from React Native, and it provides defaults that work better for Expo users.

StatusBarProps

animated

Optional • Type: boolean

If the transition between status bar property changes should be animated. Supported for style and hidden.

hidden

Optional • Type: boolean

If the status bar is hidden.

hideTransitionAnimation

Only for:
iOS

Optional • Type: StatusBarAnimation • Default: 'fade'

The transition effect when showing and hiding the status bar using the hidden prop.

style

Optional • Type: StatusBarStyle • Default: 'auto'

Sets the color of the status bar text. Default value is "auto" which picks the appropriate value according to the active color scheme, eg: if your app is dark mode, the style will be "light".

Component methods

setHidden(hidden, animation)

ParameterTypeDescription
hiddenboolean

If the status bar should be hidden.

animation(optional)StatusBarAnimation

Animation to use when toggling hidden, defaults to 'none'.


Toggle visibility of the status bar.

Returns:
void

Example

StatusBar.setHidden(true, 'slide');

setStyle(style, animated)

ParameterTypeDescription
styleStatusBarStyle

The color of the status bar text.

animated(optional)boolean

If the transition should be animated.


Set the bar style of the status bar.

Returns:
void

Example

StatusBar.setStyle('dark', true);

Methods

StatusBar.setStatusBarHidden(hidden, animation)

ParameterTypeDescription
hiddenboolean

If the status bar should be hidden.

animation(optional)StatusBarAnimation

Animation to use when toggling hidden, defaults to 'none'.


Returns:
void

StatusBar.setStatusBarStyle(style, animated)

ParameterTypeDescription
styleStatusBarStyle

The color of the status bar text.

animated(optional)boolean

If the transition should be animated.


Returns:
void

Types

StatusBarAnimation

Literal type: string

Acceptable values are: 'none' | 'fade' | 'slide'

StatusBarStyle

Literal type: string

Acceptable values are: 'auto' | 'inverted' | 'light' | 'dark'