具有灵活 API 的库,用于访问设备的安全区域插入信息。
react-native-safe-area-context
提供了灵活的 API 用于访问设备安全区域插入信息。这使你可以将内容适当地放置在缺口、状态栏、主页指示器以及其他此类设备和操作系统界面元素周围。它还提供了一个 SafeAreaView
组件,你可以使用它来代替 View
,以自动插入视图以考虑安全区域。
¥react-native-safe-area-context
provides a flexible API for accessing device safe area inset information. This allows you to position your content appropriately around notches, status bars, home indicators, and other such device and operating system interface elements. It also provides a SafeAreaView
component that you can use in place of View
to automatically inset your views to account for safe areas.
¥Installation
-
npx expo install react-native-safe-area-context
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 {
SafeAreaView,
SafeAreaProvider,
SafeAreaInsetsContext,
useSafeAreaInsets,
} from 'react-native-safe-area-context';
¥Components
SafeAreaView
SafeAreaView
是常规 View
组件,其中安全区域边缘用作填充。
¥SafeAreaView
is a regular View
component with the safe area edges applied as padding.
如果你在视图上设置自己的填充,它将被添加到安全区域的填充中。
¥If you set your own padding on the view, it will be added to the padding from the safe area.
如果你的目标是 Web,则必须按照 上下文 部分中的说明设置SafeAreaProvider
。
import { SafeAreaView } from 'react-native-safe-area-context';
function SomeComponent() {
return (
<SafeAreaView>
<View />
</SafeAreaView>
);
}
¥Props
edges
可选 • 类型:Edge[]
• 默认:["top", "right", "bottom", "left"]
¥Optional • Type: Edge[]
• Default: ["top", "right", "bottom", "left"]
设置要应用安全区域插图的边缘。
¥Sets the edges to apply the safe area insets to.
emulateUnlessSupported
Optional • Type: boolean
• Default: true
在 iOS 10+ 上,使用状态栏高度和主页指示器大小模拟安全区域。
¥On iOS 10+, emulate the safe area using the status bar height and home indicator sizes.
¥Hooks
useSafeAreaInsets()
Hook 可让你直接访问安全区域插图。这是一个更高级的用例,在旋转设备时可能比 SafeAreaView
表现更差。
¥Hook gives you direct access to the safe area insets. This is a more advanced use-case, and might perform worse than SafeAreaView
when rotating the device.
Example
import { useSafeAreaInsets } from 'react-native-safe-area-context';
function HookComponent() {
const insets = useSafeAreaInsets();
return <View style={{ paddingTop: insets.top }} />;
}
Returns
¥Types
Edge
可能的边的字符串并集。
¥String union of possible edges.
可接受的值为:'top'
、'right'
、'bottom'
、'left'
。
¥Acceptable values are: 'top'
, 'right'
, 'bottom'
, 'left'
.
EdgeInsets
代表钩子结果。
¥Represent the hook result.
EdgeInsets Properties
名称 | 类型 | 描述 |
---|---|---|
bottom | number | 底部插入的值。 |
left | number | 左插图的值。 |
right | number | 右插图的值。 |
top | number | 顶部插图的值。 |
¥Guides
¥Context
要使用安全区域上下文,你需要在应用根组件中添加 SafeAreaProvider
。
¥To use safe area context, you need to add SafeAreaProvider
in your app root component.
你可能还需要将其添加到其他位置,包括使用
react-native-screen
时在任何模式的根部或任何路由。¥You may need to add it in other places too, including at the root of any modals any routes when using
react-native-screen
.
import { SafeAreaProvider } from 'react-native-safe-area-context';
function App() {
return <SafeAreaProvider>...</SafeAreaProvider>;
}
然后,你可以使用 useSafeAreaInsets()
钩子和消费者 API 来访问插入数据:
¥Then, you can use useSafeAreaInsets()
hook and also consumer API to access inset data:
import { SafeAreaInsetsContext } from 'react-native-safe-area-context';
function Component() {
return (
<SafeAreaInsetsContext.Consumer>
{insets => <View style={{ paddingTop: insets.top }} />}
</SafeAreaInsetsContext.Consumer>
);
}
¥Optimization
如果可以的话,请使用 SafeAreaView
。它是原生实现的,因此当旋转设备时,异步桥不会产生延迟。
¥If you can, use SafeAreaView
. It's implemented natively so when rotating the device, there is no delay from the asynchronous bridge.
为了加速初始渲染,你可以从此包导入 initialWindowMetrics
并设置为提供程序上的 initialMetrics
属性,如 Web SSR 中所述。如果你的提供商重新安装,或者你正在使用 react-native-navigation
,则无法执行此操作。
¥To speed up the initial render, you can import initialWindowMetrics
from this package and set as the initialMetrics
prop on the provider as described in Web SSR. You cannot do this if your provider remounts, or you are using react-native-navigation
.
import { SafeAreaProvider, initialWindowMetrics } from 'react-native-safe-area-context';
function App() {
return <SafeAreaProvider initialMetrics={initialWindowMetrics}>...</SafeAreaProvider>;
}
¥Web SSR
如果你在网络上进行服务器端渲染,则可以使用 initialSafeAreaInsets
根据用户拥有的设备注入值,或者直接传递零。否则,插图测量将破坏页面内容的渲染,因为它是异步的。
¥If you are doing server side rendering on the web, you can use initialSafeAreaInsets
to inject values based on the device the user has, or simply pass zero. Otherwise, insets measurement will break rendering your page content since it is async.
¥Migrating from CSS
¥Before
在纯 Web 应用中,你可以使用 CSS 环境变量来获取屏幕安全区域插图的大小。
¥In a web-only app, you would use CSS environment variables to get the size of the screen's safe area insets.
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);
}
¥After
一般来说,钩子 useSafeAreaInsets()
可以提供对此信息的访问。
¥Universally, the hook useSafeAreaInsets()
can provide access to this information.
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,
}}
/>
);
}