几个月前我遇到了类似的问题,所以我写了一个工具来给我正确的顶部/底部SafeArea高度,作为填充添加到正常的 react-native View,然后通过添加/删除更多填充来使用它们。这是代码:
import { Dimensions, Platform } from 'react-native'
export function isIphoneX () {
const iphoneXLength = 812
const iphoneXSMaxLength = 896
const windowDimensions = Dimensions.get('window')
return (
Platform.OS === 'ios' &&
!Platform.isPad &&
!Platform.isTVOS &&
(windowDimensions.width === iphoneXLength ||
windowDimensions.height === iphoneXLength ||
windowDimensions.width === iphoneXSMaxLength ||
windowDimensions.height === iphoneXSMaxLength)
)
}
const DimensionsStyle = {
safeAreaTopHeight: Platform.OS === 'ios' ? (isIphoneX() ? 44 : 20) : 0,
safeAreaBottomHeight: Platform.OS === 'ios' && isIphoneX() ? 35 : 0,
tabBarHeight: Platform.OS === 'ios' ? 17 : 20,
bottomAreaHeight: Platform.OS === 'ios' && isIphoneX() ? 34 : 0
}
export default DimensionsStyle
此代码有效,因为我们知道 iPhone X 和 iPhone XS 的高度为812p,而 iPhone XSMax 和 XR 的高度为896p。
然后你可以简单地将这个工具导入到你的视图中并像这样使用它:
import Dimension from '../../../utils/DimensionUtils' // path of the util
//
// rest of the code
//
const styles = StyleSheet.create({
container: {
paddingTop: Dimension.safeAreaTopHeight // here you can edit the height of the safeare :))
}
})