This is documentation for the next SDK version. For up-to-date documentation, see the latest version (SDK 55).
Expo 状态栏
一个提供与 React Native StatusBar API 相同接口的库,但默认值略有不同,以便在 Expo 环境中更好地工作。
For the complete documentation index, see llms.txt. Use this Use this file to discover all available pages.
expo-status-bar 为你提供了一个组件和命令式接口,用于控制应用状态栏,更改其文字颜色、隐藏它,并对这些更改应用动画。你能够使用 StatusBar 组件实现的具体功能取决于你所使用的平台。
tvOS 和 web 支持
对于 tvOS,
expo-status-bar的代码可以编译并运行,但不会显示状态栏。对于 web,没有可用的 API 来控制操作系统的状态栏,因此
expo-status-bar不会执行任何操作,也不会抛出错误。
安装
- npx expo install expo-status-barIf you are installing this in an existing React Native app, make sure to install expo in your project.
在 app config 中配置
如果你在项目中使用 config plugins(Continuous Native Generation (CNG)),可以使用 expo-status-bar 内置的 config plugin 进行配置。该插件允许你配置一些无法在运行时设置、并且需要重新构建应用二进制文件才会生效的属性。如果你的应用没有使用 CNG,那么你需要手动配置该库。
Example app.json with config plugin
{ "expo": { "plugins": [ [ "expo-status-bar", { "hidden": false, "style": "dark" } ] ] } }
Configurable properties
| Name | Default | Description |
|---|---|---|
hidden | undefined | 确定状态栏是否在启动时隐藏。接受 |
style | undefined | 确定状态栏启动时使用的样式。接受 |
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/>
使用
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
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
| Parameter | Type | Description |
|---|---|---|
| hidden | boolean | If the status bar should be hidden. |
| animation(optional) | StatusBarAnimation | Animation to use when toggling hidden, defaults to |
Toggle visibility of the status bar.
voidExample
StatusBar.setHidden(true, 'slide');
| Parameter | Type | Description |
|---|---|---|
| style | StatusBarStyle | The color of the status bar text. |
| animated(optional) | boolean | If the transition should be animated. |
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.
| Parameter | Type | Description |
|---|---|---|
| hidden | boolean | If the status bar should be hidden. |
| animation(optional) | StatusBarAnimation | Animation to use when toggling hidden, defaults to |
voidDeprecated: Use
StatusBar.setStyleinstead. This will be removed in a future release.
| Parameter | Type | Description |
|---|---|---|
| style | StatusBarStyle | The color of the status bar text. |
| animated(optional) | boolean | If the transition should be animated. |
voidTypes
Literal Type: string
Acceptable values are: 'none' | 'fade' | 'slide'