Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 account?

like image 343
TIMEX Avatar asked Mar 13 '19 22:03

TIMEX


People also ask

How do you find the height and width of a view in react native?

import { Dimensions } from 'react-native'; You can get the application window's width and height using the following code: const windowWidth = Dimensions. get('window').

What is safe area view?

SafeAreaView renders nested content and automatically applies padding to reflect the view that is not covered by navigation bars, tab bars, and toolbars. Safe Area's paddings reflect the physical limitation of the screen, such as rounded corners or camera notches.

Should I use SafeAreaView?

You have to only use when any screen has headerMode:none or it out side of the navigation. If you are using full screen modal then you should use <SafeAreaView> .


2 Answers

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);
like image 156
Vinayak B Avatar answered Nov 16 '22 02:11

Vinayak B


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

like image 23
Maneesh Avatar answered Nov 16 '22 02:11

Maneesh