jas*_*san 14 ios react-native react-native-ios
对于Android我知道我可以使用,StatusBar.currentHeight 但我不知道如何为iOS做到这一点.
如何在Swift(本机)中检索大小的答案已经得到了解答,但我需要在本机应用程序中使用它.
Dav*_*ulo 26
如果您正在使用 Expo,则可以使用Constants.statusBarHeight.
import Constants from 'expo-constants';
const statusBarHeight = Constants.statusBarHeight;
Run Code Online (Sandbox Code Playgroud)
如果您使用 Vanilla React Native 和 React Navigation,您可以使用以下内容:
import { useSafeAreaInsets } from 'react-native-safe-area-context';
const insets = useSafeAreaInsets();
const statusBarHeight = insets.top;
Run Code Online (Sandbox Code Playgroud)
请参阅: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>
);
}
Run Code Online (Sandbox Code Playgroud)
输出:
| 三星 Galaxy S10 5G | iPhone 8 Plus | iPhone 11 Pro 最大 | 网络 | |
|---|---|---|---|---|
insets.top |
39.71428680419922 | 20 | 44 | 0 |
Constants.statusBarHeight |
39 | 20 | 44 | 0 |
StatusBar.currentHeight ?? 'N/A' |
39.42856979370117 | 不适用 | 不适用 | 不适用 |
实时代码: https: //snack.expo.dev/@dcangulo/statusbarheight
| 归档时间: |
|
| 查看次数: |
10423 次 |
| 最近记录: |