在反应导航中,如何获取标题和TabBar之间可见区域的尺寸?

时间:2019-03-13 22:50:54

标签: ios react-native react-navigation

const viewableWindowHeight = Dimensions.get('window')。height-Header.HEIGHT-???

如何获取TabBar的高度? 如果iPhone是X,该怎么办?我该如何考虑?

4 个答案:

答案 0 :(得分:5)

尝试一下:

import { Dimensions, Platform } from 'react-native';
import {
  getStatusBarHeight,
  getBottomSpace,
} from 'react-native-iphone-x-helper';
import { Header } from 'react-navigation';

const { height } = Dimensions.get('window');
  const stackHeaderHeight = Header.HEIGHT;

  /* Taken from source code of react-navigation-tabs*/
  const TAB_BAR_DEFAULT_HEIGHT = 49;
  const TAB_BAR_COMPACT_HEIGHT = 29;

  const TAB_BAR_HEIGHT = this.bottomTabBarRef._shouldUseHorizontalLabels() && !Platform.isPad
    ? TAB_BAR_COMPACT_HEIGHT
    : TAB_BAR_DEFAULT_HEIGHT;

  const marginTop = getStatusBarHeight() + stackHeaderHeight;
  const marginBottom = getBottomSpace() + TAB_BAR_HEIGHT;

  // < What you're after 
  const viewableWindowHight = height - marginTop - marginBottom; 

用于TBBAR

根据此方法确定的条件,高度在这两个值>> TAB_BAR_COMPACT_HEIGHTTAB_BAR_DEFAULT_HEIGHT之间变化:

根据react-navigation-tabs源代码。

OR

您可以按照documentation所述将initialLayout设置为TabNavigatorConfig

  

initialLayout-包含初始高度和   宽度,可以通过以防止延迟一帧   react-native-tab-view渲染。

对于IPHONE-X

您可以通过react-native-iphone-x-helper npm模块安全地访问Iphone-X中的statusBar高度,bottomSpace

答案 1 :(得分:4)

  

解决方案1 ​​

如果您想直接计算可见窗口的高度,则可以使用onLayout回调,例如,在每个页面的标签导航中,

 render() {
      return (
     <View style={{ flex: 1}} onLayout={(event) => {
              var {x, y, width, height} = event.nativeEvent.layout;
              this.viewableWindowHeight=height;
              // use height as viewableWindowHeight
       }} />

    <ScollView>
     //Your scrollable contant
    </ScrollView>
  </View>
);
  

解决方案2

根据反应导航中的问题,您无法直接计算底部标签栏的高度。但是,如果将底部标签栏包装到视图中,然后可以将该视图高度计算为底部标签栏。考虑下面的示例

    import React, { Component } from 'react';
import { connect } from 'react-redux';
import { View } from 'react-native';
import { BottomTabBar } from 'react-navigation';

class TabBarComponent extends Component {
  measure = () => {
    if (this.tabBar) {
      this.tabBar.measureInWindow(this.props.setTabMeasurement);
    }
  }

  render() {
    return (
      <View
        ref={(el) => { this.tabBar = el; }}
        onLayout={this.measure}
      >
        <BottomTabBar {...this.props} />
      </View>
    );
  }
}

function mapDispatchToProps(dispatch) {
  return {
    setTabMeasurement: (x, y, width, height) => dispatch({
      type: 'SET_TAB_MEASUREMENT',
      measurement: {
        x, y, width, height,
      },
    }),
  };
}

export default connect(null, mapDispatchToProps)(TabBarComponent);

答案 2 :(得分:3)

您可以简单地使用SafeAreaView来自动为iPhoneX手机自动设置topBarHeight。

答案 3 :(得分:0)

在新版本中使用

import { useHeaderHeight } from "react-navigation-stack"; console.log(useHeaderHeight());