This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.
Expo StatusBar
一个库,提供与 React Native StatusBar API 相同的接口,但默认值略有不同,能够很好地适配 Expo 环境。
expo-status-bar 提供了一个组件和命令式接口,用于控制应用状态栏的文本颜色、隐藏状态栏,以及为这些更改应用动画。具体来说,StatusBar 组件支持哪些功能取决于你使用的平台。
tvOS 和 web 支持
对于 tvOS,
expo-status-bar代码可以编译并运行,但不会显示状态栏。对于 web,没有可用于控制操作系统状态栏的 API,因此
expo-status-bar不会执行任何操作,也不会抛出错误。
安装
If you are installing this in an existing React Native app, make sure to install expo in your project.
在应用配置中配置
如果你的项目使用 config plugins(Continuous Native Generation(CNG)),则可以使用内置的 config plugin 配置 expo-status-bar。该插件允许你配置各种无法在运行时设置、且需要重新构建应用二进制文件才能生效的属性。如果你的应用不使用 CNG,则需要手动配置此库。
Example app.json with config plugin
Configurable properties
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/>
用法
API
import { StatusBar } from 'expo-status-bar';
Component
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.
booleanIf the transition between status bar property changes should be
animated. Supported for style and hidden.
StatusBarAnimation • Default: 'fade'The transition effect when showing and hiding the status bar using the hidden prop.
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
Toggle visibility of the status bar.
voidExample
StatusBar.setHidden(true, 'slide');
Set the bar style of the status bar.
voidExample
StatusBar.setStyle('dark', true);
Methods
Deprecated: Use
StatusBar.setHiddeninstead. This will be removed in a future release.
voidDeprecated: Use
StatusBar.setStyleinstead. This will be removed in a future release.
voidTypes
Literal type: string
Acceptable values are: 'none' | 'fade' | 'slide'