In react-navigation, how do I get the dimensions of the visible area between the header and TabBar?

后端 未结 4 856
鱼传尺愫
鱼传尺愫 2021-02-15 11:43

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

How do I get the TabBar height? What if the iPhone is X? How can I take that into a

相关标签:
4条回答
  • 2021-02-15 12:14

    You can simply use SafeAreaView which will automatically set topBarHeight mainly for iPhoneX phones.

    0 讨论(0)
  • 2021-02-15 12:15

    Solution 1

    If you want to calculate viewable window height directly, then you can use the onLayout callback, for eg, on tab navigation each page,

     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>
    );
    

    Solution 2

    According to an issue in react navigation, you can't directly calculate the height of the bottom tab Bar. But if you wrap bottom tab bar into a view and then you can calculate that views height as bottom tab bar. Consider the example below

        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);
    
    0 讨论(0)
  • 2021-02-15 12:29

    In the new version use

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

    0 讨论(0)
  • 2021-02-15 12:34

    Try this:

    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; 
    

    FOR TBBAR

    Height is changing between these two values >> TAB_BAR_COMPACT_HEIGHT, and TAB_BAR_DEFAULT_HEIGHT, according to a condition determined by this method:

    According to react-navigation-tabs source code.

    OR

    You could set initialLayout to your TabNavigatorConfig as mentioned in the documentation:

    initialLayout - Optional object containing the initial height and width, can be passed to prevent the one frame delay in react-native-tab-view rendering.

    FOR IPHONE-X

    You can access statusBar height, bottomSpace in Iphone-X safely though react-native-iphone-x-helper npm module

    0 讨论(0)
提交回复
热议问题