在某些非常特殊的情况下,我需要将View的高度设置为设备有用区域的整个高度(不使用flex)。
我使用了一个硬编码的“缺口高度”来计算此有用的高度,但是我发现缺口取决于设备的高度可能有所不同。(iPhone XS和iPhone XS Max之间有3点差异)。
有没有办法知道带有缺口和安全区域的设备的有用高度?
您可以使用react-native-safe-area。它提供了获取安全区域插图的顶部,底部,左侧,右侧的功能。
import SafeArea, { type SafeAreaInsets } from 'react-native-safe-area' //Retrieve safe area insets for root view SafeArea.getSafeAreaInsetsForRootView() .then((result) => { console.log(result) // { safeAreaInsets: { top: 44, left: 0, bottom: 34, right: 0 } } })