Expo NavigationBar
一个提供在 Android 上与原生导航栏进行各种交互的库。
For the complete documentation index, see llms.txt. Use this Use this file to discover all available pages.
expo-navigation-bar 使你能够修改并观察 Android 设备上的原生导航栏。由于某些 Android 平台限制,此 API 的部分功能与 expo-status-bar API 重叠。
当 Android 设备启用“手势导航”时,本包中的 API 不会产生任何影响。目前没有原生 Android API 可用于检测“手势导航”是否已启用。
安装
- npx expo install expo-navigation-barIf you are installing this in an existing React Native app, make sure to install expo in your project.
在 app 配置中进行配置
如果你在项目中使用了 config plugins(Continuous Native Generation (CNG)),可以使用 expo-navigation-bar 内置的 config plugin 来进行配置。该插件允许你配置一些无法在运行时设置、且需要重新构建新的应用二进制文件才会生效的属性。如果你的应用没有使用 CNG,那么你需要手动配置该库。
Example app.json with config plugin
{ "expo": { "plugins": [ [ "expo-navigation-bar", { "enforceContrast": true, "barStyle": "light", "visibility": "visible" } ] ] } }
Configurable properties
| Name | Default | Description |
|---|---|---|
enforceContrast | true | 决定操作系统是否应保持导航栏半透明,以便在导航按钮与应用内容之间提供对比度。 |
barStyle | undefined | 控制 Android 渲染浅色还是深色的导航栏按钮。可接受 |
visibility | undefined | 决定导航栏初始时是显示还是隐藏。可接受 |
Are you using this library in an existing React Native app?
如果你没有使用 Continuous Native Generation(CNG),或者你是手动使用原生 android 项目,那么你需要将以下配置添加到你的原生项目中:
-
要将
visibility应用于导航栏,请将expo_navigation_bar_visibility添加到 android/app/src/main/res/values/strings.xml:<resources> <!-- ... --> <string name="expo_navigation_bar_visibility" translatable="false">visible</string> </resources>
API
import * as NavigationBar from 'expo-navigation-bar';
Hooks
React hook that statefully updates with the visibility of the system navigation bar.
NavigationBarVisibility | nullVisibility of the navigation bar, null during async initialization.
Example
function App() { const visibility = NavigationBar.useVisibility() // React Component... }
Methods
Deprecated: Due to Android edge-to-edge enforcement, getting the navigation bar background color is deprecated and always returns
#00000000(transparent). This will be removed in a future release.
Gets the navigation bar's background color.
Promise<string>Current navigation bar color in hex format. Returns #00000000 (transparent) on unsupported platforms (iOS, web).
Example
const color = await NavigationBar.getBackgroundColorAsync();
Deprecated: Due to Android edge-to-edge enforcement, getting the navigation bar behavior is deprecated and always returns
inset-touch. This will be removed in a future release.
Gets the behavior of the status and navigation bars when the user swipes or touches the screen.
Promise<NavigationBarBehavior>Navigation bar interaction behavior. Returns inset-touch on unsupported platforms (iOS, web).
Example
await NavigationBar.getBehaviorAsync()
Deprecated: Due to Android edge-to-edge enforcement, getting the navigation bar border color is deprecated and always returns
#00000000(transparent). This will be removed in a future release.
Gets the navigation bar's top border color, also known as the "divider color".
Promise<string>Navigation bar top border color in hex format. Returns #00000000 (transparent) on unsupported platforms (iOS, web).
Example
const color = await NavigationBar.getBorderColorAsync();
Deprecated: Due to Android edge-to-edge enforcement, getting the navigation bar button color style is deprecated and always returns
light. This will be removed in a future release.
Gets the navigation bar's button color style.
Promise<NavigationBarButtonStyle>Navigation bar foreground element color settings. Returns light on unsupported platforms (iOS, web).
Example
const style = await NavigationBar.getButtonStyleAsync();
Get the navigation bar's visibility.
Promise<NavigationBarVisibility>Navigation bar's current visibility status. Returns hidden on unsupported platforms (iOS, web).
Example
const visibility = await NavigationBar.getVisibilityAsync();
Deprecated: Due to Android edge-to-edge enforcement, setting the navigation bar background color is deprecated and has no effect. This will be removed in a future release.
| Parameter | Type | Description |
|---|---|---|
| color | string | Any valid CSS 3 (SVG) color. |
Changes the navigation bar's background color.
Promise<void>Example
NavigationBar.setBackgroundColorAsync("white");
Deprecated: Due to Android edge-to-edge enforcement, setting the navigation bar behavior is deprecated and has no effect. This will be removed in a future release.
| Parameter | Type | Description |
|---|---|---|
| behavior | NavigationBarBehavior | Dictates the interaction behavior of the navigation bar. |
Sets the behavior of the status bar and navigation bar when they are hidden and the user wants to reveal them.
For example, if the navigation bar is hidden (setVisibilityAsync(false)) and the behavior
is 'overlay-swipe', the user can swipe from the bottom of the screen to temporarily reveal the navigation bar.
'overlay-swipe': Temporarily reveals the System UI after a swipe gesture (bottom or top) without insetting your App's content.'inset-swipe': Reveals the System UI after a swipe gesture (bottom or top) and insets your App's content (Safe Area). The System UI is visible until you explicitly hide it again.'inset-touch': Reveals the System UI after a touch anywhere on the screen and insets your App's content (Safe Area). The System UI is visible until you explicitly hide it again.
Promise<void>Example
await NavigationBar.setBehaviorAsync('overlay-swipe')
Deprecated: Due to Android edge-to-edge enforcement, setting the navigation bar border color is deprecated and has no effect. This will be removed in a future release.
| Parameter | Type | Description |
|---|---|---|
| color | string | Any valid CSS 3 (SVG) color. |
Changes the navigation bar's border color.
Promise<void>Example
NavigationBar.setBorderColorAsync("red");
Deprecated: Use
setStyleinstead. This will be removed in a future release.
| Parameter | Type | Description |
|---|---|---|
| style | NavigationBarButtonStyle | Dictates the color of the foreground element color. |
Changes the navigation bar's button colors between white (light) and a dark gray color (dark).
Promise<void>Example
NavigationBar.setButtonStyleAsync("light");
Deprecated: Due to Android edge-to-edge enforcement, setting the navigation bar position is deprecated and has no effect. This will be removed in a future release.
| Parameter | Type | Description |
|---|---|---|
| position | NavigationBarPosition | Based on CSS position property. |
Sets positioning method used for the navigation bar (and status bar).
Setting position absolute will float the navigation bar above the content,
whereas position relative will shrink the screen to inline the navigation bar.
When drawing behind the status and navigation bars, ensure the safe area insets are adjusted accordingly.
Promise<void>Example
// enables edge-to-edge mode await NavigationBar.setPositionAsync('absolute') // transparent backgrounds to see through await NavigationBar.setBackgroundColorAsync('#ffffff00')
| Parameter | Type |
|---|---|
| style | NavigationBarStyle |
Sets the style of the navigation bar.
This will have an effect when the following conditions are met:
- The
enforceContrastoption of theexpo-navigation-barplugin is set tofalse.- The device is using the three-button navigation bar.
Due to a bug in the Android 15 emulator this function may have no effect. Try a physical device or an emulator with a different version of Android.
void| Parameter | Type | Description |
|---|---|---|
| visibility | NavigationBarVisibility | Based on CSS visibility property. |
Set the navigation bar's visibility.
Promise<void>Example
NavigationBar.setVisibilityAsync("hidden");
Deprecated: Due to Android edge-to-edge enforcement, getting the navigation bar position is deprecated and always returns
relative. This will be removed in a future release.
Whether the navigation and status bars float above the app (absolute) or sit inline with it (relative).
This value can be incorrect if androidNavigationBar.visible is used instead of the config plugin position property.
This method is unstable because the position can be set via another native module and get out of sync.
Alternatively, you can get the position by measuring the insets returned by react-native-safe-area-context.
Promise<NavigationBarPosition>Navigation bar positional rendering mode. Returns relative on unsupported platforms (iOS, web).
Example
await NavigationBar.unstable_getPositionAsync()
Event Subscriptions
| Parameter | Type |
|---|---|
| listener | (event: NavigationBarVisibilityEvent) => void |
Observe changes to the system navigation bar. Due to platform constraints, this callback will also be triggered when the status bar visibility changes.
EventSubscriptionExample
NavigationBar.addVisibilityListener(({ visibility }) => { // ... });
Types
Deprecated: This will be removed in a future release.
Literal Type: string
Interaction behavior for the system navigation bar.
Acceptable values are: 'overlay-swipe' | 'inset-swipe' | 'inset-touch'
Literal Type: string
Appearance of the foreground elements in the navigation bar, i.e. the color of the menu, back, home button icons.
darkmakes buttons darker to adjust for a mostly light nav bar.lightmakes buttons lighter to adjust for a mostly dark nav bar.
Acceptable values are: 'light' | 'dark'
Deprecated: This will be removed in a future release.
Literal Type: string
Navigation bar positional mode.
Acceptable values are: 'relative' | 'absolute'
Literal Type: string
Navigation bar style.
autowill automatically adjust based on the current theme.lighta light navigation bar with dark content.darka dark navigation bar with light content.invertedthe bar colors are inverted in relation to the current theme.
Acceptable values are: 'auto' | 'inverted' | 'light' | 'dark'
Literal Type: string
Visibility of the navigation bar.
Acceptable values are: 'visible' | 'hidden'
Current system UI visibility state. Due to platform constraints, this will return when the status bar visibility changes as well as the navigation bar.
| Property | Type | Description |
|---|---|---|
| rawVisibility | number | Native Android system UI visibility state, returned from the native Android |
| visibility | NavigationBarVisibility | Current navigation bar visibility. |