如何知道React Native中iOS设备的有用高度? [英] How to know the useful height of an iOS device in React Native?

查看:116
本文介绍了如何知道React Native中iOS设备的有用高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在某些非常特殊的情况下,我需要将View的高度设置为设备有用区域的整个高度(不使用flex).

In some very specific cases I need to set the height of a View to the full height of the device useful area (without using flex).

我当时使用的是硬编码的凹口高度"来计算此有用的高度,但我刚刚发现,根据设备的不同,凹口可以具有不同的高度.(iPhone XS和iPhone XS Max相差3点).

I was using a hardcoded "notch height" to calculate this useful height but I just discovered that the notch can have different heights depending on the device. (3 points of difference between iPhone XS and iPhone XS Max).

有没有办法知道带有缺口和安全区域的设备的有用高度?

Is there a way to know the useful height of a device with notch and safe area?

推荐答案

您可以使用反应本地安全区域.它提供了获取顶部,底部,左侧,右侧的安全区域插图的功能.

You can use the react-native-safe-area. it provides function to Get safe area inset top, bottom, left, right.

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 } }
})

这篇关于如何知道React Native中iOS设备的有用高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆