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 onLayout
is measured with respect to the parent view. So i'd need to know the parent view's current height to reconcile the differences.
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.
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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With