Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Navigation get stack header height

Tags:

How do i get the height of the StackNavigator header programmatically?

I need to detect if component A's position is within component B when pan gesture (drag) on A is released. However, the position from onPanResponderRelease's gesture.moveY is measured with respect to the entire screen, whereas the position returned from A's onLayoutis measured with respect to the parent view. So i'd need to know the parent view's current height to reconcile the differences.

like image 583
Avery235 Avatar asked Aug 17 '17 12:08

Avery235


People also ask

What is Createstacknavigator?

Provides a way for your app to transition between screens where each new screen is placed on top of a stack. By default the stack navigator is configured to have the familiar iOS and Android look & feel: new screens slide in from the right on iOS, fade in from the bottom on Android.

How do you navigate in stack Navigator?

navigate ​ If the screen is not present, it'll push a new screen. For example, if you have a stack with the history Home > Profile > Settings and you call navigate(Profile) , the resulting screens will be Home > Profile as it goes back to Profile and removes the Settings screen.


2 Answers

React navigation V6

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

React navigation V5

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

or with React Context's API (not recommended)

React navigation V4

import { Header } from 'react-navigation-stack'; const headerHeight = Header.HEIGHT; 

React navigation V2-V3

import { Header } from 'react-navigation'; const headerHeight = Header.HEIGHT; 

Another answer to this problem

like image 98
Florent Roques Avatar answered Sep 18 '22 15:09

Florent Roques


These are the helpers that I use. getHeaderHeight method gets the correct height in every platform (including iphone x) and orientation:

import { Dimensions, DeviceInfo, Platform } from 'react-native'; import { Header } from 'react-navigation';  export const LANDSCAPE = 'landscape'; export const PORTRAIT = 'portrait';  export const getHeaderHeight = () => {   let height;   const orientation = getOrientation();   height = getHeaderSafeAreaHeight();   height += DeviceInfo.isIPhoneX_deprecated && orientation === PORTRAIT ? 24 : 0;    return height; };  // This does not include the new bar area in the iPhone X, so I use this when I need a custom headerTitle component export const getHeaderSafeAreaHeight = () => {   const orientation = getOrientation();   if (Platform.OS === 'ios' && orientation === LANDSCAPE && !Platform.isPad) {     return 32;   }   return Header.HEIGHT; };  export const getOrientation = () => {   const { width, height } = Dimensions.get('window');   return width > height ? LANDSCAPE : PORTRAIT; }; 

https://github.com/react-navigation/react-navigation/issues/2411#issuecomment-382434542

like image 23
Javier Orellana Avatar answered Sep 20 '22 15:09

Javier Orellana