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

TIM*_*MEX 7 ios react-native react-navigation

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

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

Vin*_*k B 5

解决方案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>
);
Run Code Online (Sandbox Code Playgroud)

解决方案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);
Run Code Online (Sandbox Code Playgroud)


Hen*_*hli 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; 
Run Code Online (Sandbox Code Playgroud)

对于TBBAR

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

根据react-navigation-tabs源代码。

要么

你可以设置initialLayoutTabNavigatorConfig作为中提到的文档

initialLayout-可以传递包含初始高度和宽度的可选对象,以防止react-native-tab-view渲染中出现一帧延迟。

对于IPHONE-X

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