如何通过React Native找到Android中状态栏的高度?

r8uurelv  于 2023-01-14  发布在  React
关注(0)|答案(9)|浏览(361)

如何通过React Native找到Android上状态栏的高度?
如果我检查React.Dimensions高度,该值似乎也包括状态栏高度,但我试图找到没有状态栏的布局高度。

qyzbxkaa

qyzbxkaa1#

你不再需要任何插件了,只需要使用StatusBar.currentHeight

import {StatusBar} from 'react-native';
console.log('statusBarHeight: ', StatusBar.currentHeight);

编辑:显然这似乎在Android上总是有效的,但是在iOS上,最初确实返回了undefined:(

jljoyd4f

jljoyd4f2#

不幸的是,从v0.34开始,执行此操作的API在不同平台之间仍然不一致。StatusBarManager.HEIGHT将为您提供Android上状态栏的当前高度

import { Platform, NativeModules } from 'react-native';
const { StatusBarManager } = NativeModules;

const STATUSBAR_HEIGHT = Platform.OS === 'ios' ? 20 : StatusBarManager.HEIGHT;

在iOS上,您可以使用getHeight()

StatusBarManager.getHeight((statusBarHeight)=>{
  console.log(statusBarHeight)
})

顺便说一句,如果您希望您的应用在Android上的状态栏下绘制,类似于默认的iOS行为,您可以通过在<StatusBar>上设置几个属性来实现,如下所示:

<StatusBar translucent={true} backgroundColor={'transparent'} {...props} />
lf5gs5x2

lf5gs5x23#

你可以使用这个辅助函数来获取iOS和Android上的状态栏高度。对于iOS,当使用〉= iPhone X(带缺口)时,计算会得到不同的状态栏高度。

// functions-file.js
import { Dimensions, Platform, StatusBar } from 'react-native';

const X_WIDTH = 375;
const X_HEIGHT = 812;

const XSMAX_WIDTH = 414;
const XSMAX_HEIGHT = 896;

const { height, width } = Dimensions.get('window');

export const isIPhoneX = () => Platform.OS === 'ios' && !Platform.isPad && !Platform.isTVOS
    ? width === X_WIDTH && height === X_HEIGHT || width === XSMAX_WIDTH && height === XSMAX_HEIGHT
    : false;

export const StatusBarHeight = Platform.select({
    ios: isIPhoneX() ? 44 : 20,
    android: StatusBar.currentHeight,
    default: 0
})

然后在导入时直接使用:

import { StatusBarHeight } from './functions-file.js'

height: StatusBarHeight

如果使用react-navigation-stack,则另一个选项-它通过挂钩提供顶盖高度-https://reactnavigation.org/docs/en/stack-navigator.html#headertransparent

import { useHeaderHeight } from 'react-navigation-stack';

// ...

const headerHeight = useHeaderHeight();
1tuwyuhd

1tuwyuhd4#

导入常量尺寸

import Constants from 'expo-constants';
import Dimensions  from 'react-native';

在样式表中容器高度-状态栏高度=无状态高度

container: {   height: Dimensions.get('window').height - Constants.statusBarHeight,}
imzjd6km

imzjd6km5#

如果您使用的是@react-navigation/native-stack

import { useHeaderHeight } from '@react-navigation/elements';
const headerHeight = useHeaderHeight();

链接到源

dy1byipe

dy1byipe6#

import {NativeModules} from 'react-native';

// ...

const {StatusBarManager} = NativeModules;

const height = StatusBarManager.HEIGHT;
8tntrjer

8tntrjer7#

对于需要转义StatusBar组件的情况,我在Android上的解决方案是使用StatusBar.currentHeight来转义StatusBar组件,在iOS上我使用react-native的SafeAreaView组件来转义StatusBar。

g52tjvyc

g52tjvyc8#

react-native-status-bar-height工作得很好,但是,如果您不希望有其他依赖项,我在这里提取了要点:

创建文件getStatusBarHeight.tsx

import {Dimensions, Platform, StatusBar} from 'react-native';

const STATUSBAR_DEFAULT_HEIGHT = 20;
const STATUSBAR_X_HEIGHT = 44;
const STATUSBAR_IP12_HEIGHT = 47;
const STATUSBAR_IP12MAX_HEIGHT = 47;
const STATUSBAR_IP14PRO_HEIGHT = 49;

const X_WIDTH = 375;
const X_HEIGHT = 812;

const XSMAX_WIDTH = 414;
const XSMAX_HEIGHT = 896;

const IP12_WIDTH = 390;
const IP12_HEIGHT = 844;

const IP12MAX_WIDTH = 428;
const IP12MAX_HEIGHT = 926;

const IP14PRO_WIDTH = 393;
const IP14PRO_HEIGHT = 852;

const IP14PROMAX_WIDTH = 430;
const IP14PROMAX_HEIGHT = 932;

const {height: W_HEIGHT, width: W_WIDTH} = Dimensions.get('window');

let statusBarHeight = STATUSBAR_DEFAULT_HEIGHT;

if (Platform.OS === 'ios' && !Platform.isPad && !Platform.isTVOS) {
  if (W_WIDTH === X_WIDTH && W_HEIGHT === X_HEIGHT) {
    statusBarHeight = STATUSBAR_X_HEIGHT;
  } else if (W_WIDTH === XSMAX_WIDTH && W_HEIGHT === XSMAX_HEIGHT) {
    statusBarHeight = STATUSBAR_X_HEIGHT;
  } else if (W_WIDTH === IP12_WIDTH && W_HEIGHT === IP12_HEIGHT) {
    statusBarHeight = STATUSBAR_IP12_HEIGHT;
  } else if (W_WIDTH === IP12MAX_WIDTH && W_HEIGHT === IP12MAX_HEIGHT) {
    statusBarHeight = STATUSBAR_IP12MAX_HEIGHT;
  } else if (W_WIDTH === IP14PROMAX_WIDTH && W_HEIGHT === IP14PROMAX_HEIGHT) {
    statusBarHeight = STATUSBAR_IP14PRO_HEIGHT;
  } else if (W_WIDTH === IP14PRO_WIDTH && W_HEIGHT === IP14PRO_HEIGHT) {
    statusBarHeight = STATUSBAR_IP14PRO_HEIGHT;
  }
}

export function getStatusBarHeight() {
  return Platform.select({
    ios: statusBarHeight,
    android: StatusBar.currentHeight,
    default: 0,
  });
}

用法:

import {getStatusBarHeight} from './getStatusBarHeight';

export const Test = () => {

  const statusBarHeight = getStatusBarHeight();

  return <Text style={{ marginTop: statusBarHeight }}>Test</Text>

}
ctehm74n

ctehm74n9#

const deviceHeight = Dimensions.get('screen').height;
deviceHeight={deviceHeight}

相关问题