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.

react-native-safe-area-context

一个提供灵活 API 用于访问设备安全区域内边距信息的库。

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

react-native-safe-area-context 提供了灵活的 API,用于访问设备安全区域的内边距信息。这使你可以围绕刘海、状态栏、主屏幕指示器以及其他设备和操作系统界面元素妥善放置内容。它还提供了 SafeAreaView 组件,你可以用它替代 View,自动为视图添加内边距以适应安全区域。

安装

Terminal
- npx expo install react-native-safe-area-context
- yarn expo install react-native-safe-area-context
- pnpm expo install react-native-safe-area-context
- bun expo install react-native-safe-area-context

If you are installing this in an existing React Native app, make sure to install expo in your project. Then, follow the installation instructions provided in the library's README or documentation.

API

import { SafeAreaView, SafeAreaProvider, SafeAreaInsetsContext, useSafeAreaInsets, } from 'react-native-safe-area-context';

组件

SafeAreaView

SafeAreaView 是一个常规的 View 组件,会将安全区域边缘应用为内边距。

如果你为视图设置了自己的内边距,它会与安全区域的内边距叠加。

import { SafeAreaView } from 'react-native-safe-area-context'; function SomeComponent() { return ( <SafeAreaView> <View /> </SafeAreaView> ); }

SafeAreaView Props

edges

Optional • Type: Edge[] • Default: ["top", "right", "bottom", "left"]


设置要应用安全区域内边距的边缘。

emulateUnlessSupported

Optional • Type: boolean • Default: true


在 iOS 10 及更高版本中,使用状态栏高度和主屏幕指示器尺寸模拟安全区域。

Hooks

useSafeAreaInsets()

这个 Hook 可让你直接访问安全区域内边距。这属于较高级的用法,在旋转设备时可能不如 SafeAreaView 性能好。

示例

import { useSafeAreaInsets } from 'react-native-safe-area-context'; function HookComponent() { const insets = useSafeAreaInsets(); return <View style={{ paddingTop: insets.top }} />; }

返回值

EdgeInsets

类型

Edge

可能边缘值的字符串联合类型。

可接受的值为:'top'、'right'、'bottom'、'left'。

EdgeInsets

表示 Hook 的结果。

EdgeInsets 属性

名称类型描述
bottomnumber底部内边距的值。
leftnumber左侧内边距的值。
rightnumber右侧内边距的值。
topnumber顶部内边距的值。

指南

上下文

要使用安全区域上下文,需要在应用的根组件中添加 SafeAreaProvider。

import { SafeAreaProvider } from 'react-native-safe-area-context'; function App() { return <SafeAreaProvider>...</SafeAreaProvider>; }

然后,你可以使用 useSafeAreaInsets() Hook,也可以使用消费者 API 来访问内边距数据:

import { SafeAreaInsetsContext } from 'react-native-safe-area-context'; function Component() { return ( <SafeAreaInsetsContext.Consumer> {insets => <View style={{ paddingTop: insets.top }} />} </SafeAreaInsetsContext.Consumer> ); }

优化

如果可以,请使用 SafeAreaView。它是原生实现的,因此旋转设备时不会因异步桥接而产生延迟。

要加快首次渲染速度,可以从此程序包导入 initialWindowMetrics,并按照 Web SSR 部分的说明,将其设置为 Provider 的 initialMetrics 属性。如果 Provider 会重新挂载,或者你使用的是 react-native-navigation,则无法这样做。

import { SafeAreaProvider, initialWindowMetrics } from 'react-native-safe-area-context'; function App() { return <SafeAreaProvider initialMetrics={initialWindowMetrics}>...</SafeAreaProvider>; }

Web SSR

如果你要在 Web 上进行服务器端渲染,可以使用 initialSafeAreaInsets,根据用户所用设备注入相应的值,也可以直接传入零。否则,由于内边距测量是异步的,会导致页面内容渲染出错。

从 CSS 迁移

之前

在仅面向 Web 的应用中,你可以使用 CSS 环境变量获取屏幕安全区域内边距的大小。

styles.css
div { padding-top: env(safe-area-inset-top); padding-left: env(safe-area-inset-left); padding-bottom: env(safe-area-inset-bottom); padding-right: env(safe-area-inset-right); }

之后

在所有平台上,都可以使用 Hook useSafeAreaInsets() 访问这些信息。

App.js
import { useSafeAreaInsets } from 'react-native-safe-area-context'; function App() { const insets = useSafeAreaInsets(); return ( <View style={{ paddingTop: insets.top, paddingLeft: insets.left, paddingBottom: insets.bottom, paddingRight: insets.right, }} /> ); }