对于Android,我知道我可以使用StatusBar.currentHeight
,但是我不确定如何在iOS中使用。
如何在Swift(原生)中检索大小的答案已经被回答了,但我需要在React Native应用程序中使用它。
对于Android,我知道我可以使用StatusBar.currentHeight
,但是我不确定如何在iOS中使用。
如何在Swift(原生)中检索大小的答案已经被回答了,但我需要在React Native应用程序中使用它。
如果你正在使用Expo,你可以使用Constants.statusBarHeight
。
import Constants from 'expo-constants';
const statusBarHeight = Constants.statusBarHeight;
如果您正在使用原生 React Native 和 React Navigation,则可以使用以下内容:
import { useSafeAreaInsets } from 'react-native-safe-area-context';
const insets = useSafeAreaInsets();
const statusBarHeight = insets.top;
参见:https://reactnavigation.org/docs/handling-safe-area/#use-the-hook-for-more-control
示例代码:
import * as React from 'react';
import { Text, View, StatusBar } from 'react-native';
import Constants from 'expo-constants';
import { useSafeAreaInsets, SafeAreaProvider } from 'react-native-safe-area-context';
export default function App() {
return (
<SafeAreaProvider>
<ChildScreen />
</SafeAreaProvider>
);
}
function ChildScreen() {
const insets = useSafeAreaInsets();
return (
<View style={{ flex: 1, justifyContent: 'center'}}>
<Text>
{insets.top}
</Text>
<Text>
{Constants.statusBarHeight}
</Text>
<Text>
{StatusBar.currentHeight ?? 'N/A'}
</Text>
</View>
);
}
输出:
Samsung Galaxy S10 5G | iPhone 8 Plus | iPhone 11 Pro Max | Web | |
---|---|---|---|---|
insets.top |
39.71428680419922 | 20 | 44 | 0 |
Constants.statusBarHeight |
39 | 20 | 44 | 0 |
StatusBar.currentHeight ?? 'N/A' |
39.42856979370117 | N/A | N/A | N/A |
您可以使用已经支持iPhone X的React Navigation库。
即使由于某些原因您不想使用该库,您仍然可以查看源代码以复制实现到您的代码中。