我正在尝试使用React Native的StatusBar.currentHeight获取Android设备上的状态栏高度https://facebook.github.io/react-native/docs/statusbar。在Pixel 2上,该变量为24,而实际上状态栏高度为19。是否有人知道如何在所有设备上使用React Native获取正确的状态栏高度呢?
我正在尝试使用React Native的StatusBar.currentHeight获取Android设备上的状态栏高度https://facebook.github.io/react-native/docs/statusbar。在Pixel 2上,该变量为24,而实际上状态栏高度为19。是否有人知道如何在所有设备上使用React Native获取正确的状态栏高度呢?
expo-constants
模块。import Constants from 'expo-constants';
...
const STATUSBAR_HEIGHT = Constants.statusBarHeight
编辑
或者
您可以使用react-native-safe-area-context
import { useSafeAreaInsets } from 'react-native-safe-area-context';
function HookComponent() {
const insets = useSafeAreaInsets();
useEffect(() => {
const { top , bottom , left , right } = insets // typeof { top: number, right: number, bottom: number, left: number }
console.log(top , bottom , left , right)
},[insets])
return <View style={{ paddingBottom: Math.max(insets.bottom, 16) }} />;
}
import { Dimensions, StatusBar } from 'react-native'
const { height: SCREEN_HEIGHT, width: SCREEN_WIDTH } = Dimensions.get('window');
const STATUSBAR_HEIGHT = StatusBar.currentHeight;
StatusBar.currentHeight
的值是 49.09090805053711,但实际上在 Pixel 4 模拟器上应该是 62。你最终是如何解决这个问题的? - ThaJay