一个库,提供对 Android 上原生导航栏的各种交互的访问。
expo-navigation-bar
使你能够修改和观察 Android 设备上的原生导航栏。由于某些 Android 平台限制,该 API 的部分内容与 expo-status-bar
API 重叠。
¥expo-navigation-bar
enables you to modify and observe the native navigation bar on Android devices. Due to some Android platform restrictions, parts of this API overlap with the expo-status-bar
API.
属性以样式属性命名;可见性、位置、背景颜色、边框颜色等。
¥Properties are named after style properties; visibility, position, backgroundColor, borderColor, and so on.
当 Android 设备上启用 "手势导航" 时,此包中的 API 不会产生任何影响。目前没有原生 Android API 来检测 "手势导航" 是否启用。
¥The APIs in this package have no impact when "Gesture Navigation" is enabled on the Android device. There is currently no native Android API to detect if "Gesture Navigation" is enabled or not.
¥Installation
-
npx expo install expo-navigation-bar
If you are installing this in an existing React Native app (bare workflow), start by installing expo
in your project. Then, follow the additional instructions as mentioned by library's README under "Installation in bare React Native projects" section.
import * as NavigationBar from 'expo-navigation-bar';
useVisibility()
React hook that statefully updates with the visibility of the system navigation bar.
NavigationBarVisibility | null
Visibility of the navigation bar, null
during async initialization.
Example
function App() {
const visibility = NavigationBar.useVisibility()
// React Component...
}
NavigationBar.getBackgroundColorAsync()
Gets the navigation bar's background color.
Promise<ColorValue>
Current navigation bar color in hex format. Returns #00000000
(transparent) on unsupported platforms (iOS, web).
Example
const color = await NavigationBar.getBackgroundColorAsync();
NavigationBar.getBehaviorAsync()
Gets the behavior of the status and navigation bars when the user swipes or touches the screen.
Navigation bar interaction behavior. Returns inset-touch
on unsupported platforms (iOS, web).
Example
await NavigationBar.getBehaviorAsync()
NavigationBar.getBorderColorAsync()
Gets the navigation bar's top border color, also known as the "divider color".
Promise<ColorValue>
Navigation bar top border color in hex format. Returns #00000000
(transparent) on unsupported platforms (iOS, web).
Example
const color = await NavigationBar.getBorderColorAsync();
NavigationBar.getButtonStyleAsync()
Gets the navigation bar's button color styles.
Navigation bar foreground element color settings. Returns light
on unsupported platforms (iOS, web).
Example
const style = await NavigationBar.getButtonStyleAsync();
NavigationBar.getVisibilityAsync()
Get the navigation bar's visibility.
Navigation bar's current visibility status. Returns hidden
on unsupported platforms (iOS, web).
Example
const visibility = await NavigationBar.getVisibilityAsync("hidden");
NavigationBar.setBackgroundColorAsync(color)
Name | Type | Description |
---|---|---|
color | ColorValue | Any valid CSS 3 (SVG) color. |
Changes the navigation bar's background color.
Promise<void>
Example
NavigationBar.setBackgroundColorAsync("white");
NavigationBar.setBehaviorAsync(behavior)
Name | 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')
NavigationBar.setBorderColorAsync(color)
Name | Type | Description |
---|---|---|
color | ColorValue | Any valid CSS 3 (SVG) color. |
Changes the navigation bar's border color.
Promise<void>
Example
NavigationBar.setBorderColorAsync("red");
NavigationBar.setButtonStyleAsync(style)
Name | 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");
NavigationBar.setPositionAsync(position)
Name | 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')
NavigationBar.setVisibilityAsync(visibility)
Name | Type | Description |
---|---|---|
visibility | NavigationBarVisibility | Based on CSS visibility property. |
Set the navigation bar's visibility.
Promise<void>
Example
NavigationBar.setVisibilityAsync("hidden");
NavigationBar.unstable_getPositionAsync()
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
.
Navigation bar positional rendering mode. Returns relative
on unsupported platforms (iOS, web).
Example
await NavigationBar.unstable_getPositionAsync()
NavigationBar.addVisibilityListener(listener)
Name | 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.
Example
NavigationBar.addVisibilityListener(({ visibility }) => {
// ...
});
NavigationBarBehavior
Literal Type: string
Interaction behavior for the system navigation bar.
Acceptable values are: 'overlay-swipe'
| 'inset-swipe'
| 'inset-touch'
NavigationBarButtonStyle
Literal Type: string
Appearance of the foreground elements in the navigation bar, i.e. the color of the menu, back, home button icons.
dark
makes buttons darker to adjust for a mostly light nav bar.light
makes buttons lighter to adjust for a mostly dark nav bar.Acceptable values are: 'light'
| 'dark'
NavigationBarPosition
Literal Type: string
Navigation bar positional mode.
Acceptable values are: 'relative'
| 'absolute'
NavigationBarVisibility
Literal Type: string
Visibility of the navigation bar.
Acceptable values are: 'visible'
| 'hidden'
NavigationBarVisibilityEvent
Current system UI visibility state. Due to platform constraints, this will return when the status bar visibility changes as well as the navigation bar.
Name | Type | Description |
---|---|---|
rawVisibility | number | Native Android system UI visibility state, returned from the native Android |
visibility | NavigationBarVisibility | Current navigation bar visibility. |