安全区域
了解如何在 Expo 项目中添加屏幕组件的安全区域。
创建安全区域可确保应用屏幕的内容正确定位。这意味着内容不会被缺口、状态栏、主屏幕指示器以及设备物理硬件或操作系统控制的其他界面元素覆盖。当内容被覆盖时,它会被这些界面元素遮挡。
🌐 Creating a safe area ensures your app screen's content is positioned correctly. This means it doesn't get overlapped by notches, status bars, home indicators, and other interface elements that are part of the device's physical hardware or are controlled by the operating system. When the content gets overlapped, it gets concealed by these interface elements.
这是一个示例,展示了在 Android 上应用屏幕内容被状态栏遮挡的情况。在 iOS 上,相同的内容则会被圆角、刘海和状态栏遮挡。
🌐 Here's an example of an app screen's content getting concealed by the status bar on Android. On iOS, the same content is concealed by rounded corners, notch, and the status bar.
使用 react-native-safe-area-context 库
🌐 Use react-native-safe-area-context library
react-native-safe-area-context 提供了一个灵活的 API,用于处理 Android 和 iOS 设备的安全区域边距。它还提供了一个 SafeAreaView 组件,你可以使用它来代替 <View>,从而在你的屏幕组件中自动考虑安全区域。
使用该库,上一个示例的结果会发生变化,因为它会在安全区域内显示内容,如下所示:
🌐 Using the library, the result of the previous example changes as it displays the content inside a safe area, as shown below:
安装
🌐 Installation
如果你使用默认模板创建了项目,可以跳过安装 react-native-safe-area-context。该库作为 Expo Router 库的同伴依赖进行安装。否则,请运行以下命令进行安装:
🌐 You can skip installing react-native-safe-area-context if you have created a project using the default template. This library is installed as peer dependency for Expo Router library. Otherwise, install it by running the following command:
- npx expo install react-native-safe-area-context用法
🌐 Usage
你可以直接使用 SafeAreaView 来封装屏幕组件的内容。它是一个常规的 <View>,并在其上应用了安全区域插入,作为额外的内边距或外边距。
🌐 You can directly use SafeAreaView to wrap the content of your screen's component. It is a regular <View> with the safe area insets applied as extra padding or margin.
import { Text } from 'react-native'; import { SafeAreaView } from 'react-native-safe-area-context'; export default function HomeScreen() { return ( <SafeAreaView style={{ flex: 1 }}> <Text>Content is in safe area.</Text> </SafeAreaView> ); }
使用不同的 Expo 模板但没有安装 Expo Router?
在你的屏幕组件中使用 SafeAreaView 之前,将 SafeAreaProvider 导入并添加到根组件文件(例如 App.tsx)中。
🌐 Import and add SafeAreaProvider to the root component file (such as App.tsx) before using SafeAreaView in your screen component.
import { SafeAreaProvider } from 'react-native-safe-area-context'; export default function App() { return ( return <SafeAreaProvider>...</SafeAreaProvider>; ); }
备用:useSafeAreaInsets 钩子
🌐 Alternate: useSafeAreaInsets hook
作为 SafeAreaView 的替代,你可以在你的屏幕组件中使用 useSafeAreaInsets 钩子。它提供对安全区域插入的直接访问,允许你使用该钩子的插入值为 <View> 的每个边应用填充。
🌐 Alternate to SafeAreaView, you can use useSafeAreaInsets hook in your screen component. It provides direct access to the safe area insets, allowing you to apply padding for each edge of the <View> using an inset from this hook.
下面的示例使用了 useSafeAreaInsets 钩子。它使用 insets.top 对 <View> 应用顶部内边距。
🌐 The example below uses the useSafeAreaInsets hook. It applies top padding to a <View> using insets.top.
import { Text, View } from 'react-native'; import { useSafeAreaInsets } from 'react-native-safe-area-context'; export default function HomeScreen() { const insets = useSafeAreaInsets(); return ( <View style={{ flex: 1, paddingTop: insets.top }}> <Text>Content is in safe area.</Text> </View> ); }
该钩子在以下对象中提供插入项:
🌐 The hook provides the insets in the following object:
{ top: number, right: number, bottom: number, left: number }
附加信息
🌐 Additional information
最小的例子
🌐 Minimal example
下面是一个使用 useSafeAreaInsets 钩子为视图应用顶部内边距的最小工作示例。
🌐 Below is a minimal working example that uses the useSafeAreaInsets hook to apply top padding to a view.
import { Text, View } from 'react-native'; import { SafeAreaProvider, useSafeAreaInsets } from 'react-native-safe-area-context'; function HomeScreen() { const insets = useSafeAreaInsets(); return ( <View style={{ flex: 1, paddingTop: insets.top }}> <Text style={{ fontSize: 28 }}>Content is in safe area.</Text> </View> ); } export default function App() { return ( <SafeAreaProvider> <HomeScreen /> </SafeAreaProvider> ); }
与 React 导航一起使用
🌐 Usage with React Navigation
默认情况下,React Navigation 支持安全区域,并将 react-native-safe-area-context 用作对等依赖。更多信息,请参阅 React Navigation 文档。
🌐 By default, React Navigation supports safe areas and uses react-native-safe-area-context as a peer dependency. For more information, see the React Navigation documentation.
与 Web 一起使用
🌐 Usage with web
如果你面向网页,请按照使用部分中的说明设置 SafeAreaProvider。如果你进行服务器端渲染(SSR),请参阅库文档中的Web SSR部分。
🌐 If you are targeting the web, set up SafeAreaProvider as described in the usage section. If you are doing server-side rendering (SSR), see the Web SSR section in the library's documentation.