Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set header height in headerStyle in native-stack? #10097

Closed
3 of 11 tasks
gonzalo-rivas opened this issue Nov 3, 2021 · 27 comments
Closed
3 of 11 tasks

Set header height in headerStyle in native-stack? #10097

gonzalo-rivas opened this issue Nov 3, 2021 · 27 comments

Comments

@gonzalo-rivas
Copy link

gonzalo-rivas commented Nov 3, 2021

Current behavior

I am migrating to react navigation 6 and I want to use the new standard native-stack library to upgrade the performance of these stacks. I have from react-navigation 5 this stack navigator (a snippet):

Screen1StackNavigator:
return ( <Stack.Navigator screenOptions={{ headerStyle: { height: 100, backgroundColor: 'black', }, }} > <Stack.Screen name="Screen1" component={Screen1} options={{ headerTitle: () => <Title title={'Screen1'} />, }} /> </Stack.Navigator> )

This snippet is in this tree of the main navigator:
MainTabsNavigator:

<Tab.Navigator screenOptions={{ lazy: true, headerShown: false, }} ... > <Tab.Screen name="Screen1" component={Screen1StackNavigator} options={{ TabLabel: t('markets.title'), }} />...

But with this native-stack, the header style cant set its height. I saw in the docs that the only props you can set are:

Style object for header. Supported properties:

backgroundColor

So I want to know if there is a hidden tricky way to set the height as I used to do in react navigation 5 with "default" stack.

Expected behavior

Set the inner Stack Header Height to a specific height.

Reproduction

Platform

  • Android
  • iOS
  • Web
  • Windows
  • MacOS

Packages

  • @react-navigation/bottom-tabs
  • @react-navigation/drawer
  • @react-navigation/material-bottom-tabs
  • @react-navigation/material-top-tabs
  • @react-navigation/stack
  • @react-navigation/native-stack

Environment

"@react-navigation/bottom-tabs": "^6.0.9",
"@react-navigation/drawer": "^6.1.8",
"@react-navigation/elements": "^1.2.1",
"@react-navigation/native": "^6.0.6",
"@react-navigation/native-stack": "^6.2.5",
"@react-navigation/stack": "^6.0.11",

@github-actions
Copy link

github-actions bot commented Nov 3, 2021

Hey! Thanks for opening the issue. The issue doesn't seem to contain a link to a repro (a snack.expo.dev link or link to a GitHub repo under your username).

Can you provide a minimal repro which demonstrates the issue? A repro will help us debug the issue faster. Please try to keep the repro as small as possible and make sure that we can run it without additional setup.

@github-actions
Copy link

github-actions bot commented Nov 3, 2021

Couldn't find version numbers for the following packages in the issue:

  • @react-navigation/native
  • @react-navigation/bottom-tabs
  • @react-navigation/drawer
  • @react-navigation/material-bottom-tabs
  • @react-navigation/material-top-tabs
  • @react-navigation/stack

Can you update the issue to include version numbers for those packages? The version numbers must match the format 1.2.3.

@github-actions
Copy link

github-actions bot commented Nov 3, 2021

Couldn't find version numbers for the following packages in the issue:

  • @react-navigation/material-bottom-tabs
  • @react-navigation/material-top-tabs

Can you update the issue to include version numbers for those packages? The version numbers must match the format 1.2.3.

@tylercote
Copy link

+1 I am also seeing this issue going from v5 stack to v6 native-stack. Setting height on the headerStyle is no longer applying.

@matias-calvo-91
Copy link

I found a workaround for this issue.
If you use the header prop, you can set height specifying the whole component; like this:

<Stack.Screen
  options={
    header: () =>
        (
          <View style={{ height: 100 }}>
            <BackArrow navigation={navigation.goBack} style={{ position: 'absolute', left: 30, bottom: 35 }} />
            <View>
              <Text>TITLE</Text>
              <Text>TITLE</Text>
            </View>
          </View>
        ),
  }
/>

But you would need to add the back arrow manually.
It's not a solution, but at least it helped me.

@github-actions
Copy link

github-actions bot commented Jan 3, 2022

Hello 👋, this issue has been open for more than a month without a repro or any activity. If the issue is still present in the latest version, please provide a repro or leave a comment within 7 days to keep it open, otherwise it will be closed automatically. If you found a solution or workaround for the issue, please comment here for others to find. If this issue is critical for you, please consider sending a pull request to fix it.

@liquidvisual
Copy link

liquidvisual commented Mar 9, 2022

@matias-calvo-91 Thanks! That worked for me. Do you know if it's possible to make the custom header absolute though? When I do so it disappears.

@github-actions
Copy link

github-actions bot commented Mar 9, 2022

Hey! This issue is closed and isn't watched by the core team. You are welcome to discuss the issue with others in this thread, but if you think this issue is still valid and needs to be tracked, please open a new issue with a repro.

@AshotN
Copy link

AshotN commented Mar 30, 2022

Setting the header height in the header prop doesn't automatically push the content down.

@dhatGuy
Copy link

dhatGuy commented Apr 10, 2022

#10113 was supposed to fix this but setting height only works for web.

@Aure77
Copy link

Aure77 commented Apr 29, 2022

#10113 was supposed to fix this but setting height only works for web.

Only works if you use custom header.

@mcatalano54
Copy link

setting the "header" prop in options, the height changes correctly, but the "headerLeft" and "headerRight" props stopping working. I defined some icons there. So if I'm not wrong, I'll need to add that icons inside the custom view defined in the "header" option.

@chriscoomber
Copy link

To anyone stumbling across this, I managed to get a workaround that worked for me which I can share.

Whilst setting the height via options.headerStyle is not officially supported (only backgroundColor is mentioned in the docs), it does basically work.

But with the following gotcha:

The header height includes any insets due to the StatusBar, or any other notches. So, naively setting the header height to what you want it to be might cause issues on Android. If you want to set the header height to 100, you probably want to do something like:

import { useSafeAreaInsets } from 'react-native-safe-area-context';

...

const insets = useSafeAreaInsets();

...

<RootStack.Navigator
      initialRouteName="Home"
    >
      <RootStack.Screen
        name="Home"
        component={HomeScreen}
        options={{
          headerStyle: {
            height: 100 + insets.top
          }
        }}
      />

...

I wanted to scale the header height depending on the screen size, for a fluid UI (don't ask...), and for me it was helpful to know the header's default height (i.e. what it is if you don't set it). To calculate this you can do:

import { useWindowDimensions } from 'react-native';
import { useSafeAreaInsets } from 'react-native-safe-area-context';
import { getDefaultHeaderHeight } from '@react-navigation/elements';

...

const dimensions = useWindowDimensions();
const insets = useSafeAreaInsets();
const defaultHeaderHeight = getDefaultHeaderHeight(dimensions, false, insets.top);  // assuming your header never appears on a modal screen, if it does the middle argument should be true
const newHeaderHeight = myCustomScalingCode(defaultHeaderHeight - insets.top) + insets.top;

@Aure77
Copy link

Aure77 commented May 19, 2022

const headerHeight = header ? customHeaderHeight : defaultHeaderHeight;

Header height is managed here. It use default height when no custom header is provided...
It doesn't seems possible to change it...

@avanavana
Copy link

avanavana commented Sep 14, 2022

This has been an issue forever, please address it!

Nothing in the docs admits that the height of the header cannot be changed, except by passing a completely custom header component, and then needing to reimplement all the react navigation built-in functionality for gesture, animation, etc, etc. Furthermore, when you pass a custom header component, it becomes impossible to keep the header above the content of the screen, even with messing with zIndex.

This is a huge headache and something that should be extremely basic with the library.

@tsheaff
Copy link

tsheaff commented Nov 1, 2022

Damn, am I understanding this correctly that there is no way to change the height of the header other than a completely custom from-scratch header component?

@endcycles
Copy link

Question: How can I change the height of the nav component in React Navigation 6 with the new native-stack library?

Current behavior:
I am currently exploring React Navigation 6 and using the native-stack library for my navigation. I have created a stack navigator with a header style that includes a specific height using the headerStyle property. However, the height property doesn't seem to have any effect on the header in the new native-stack. Is there a way to set the height of the header in React Navigation 6 with native-stack?

Expected behavior:
I want to set the height of the inner Stack header to a specific value.

Platform:
iOS
MacOS

Packages:
@react-navigation/native-stack
@react-navigation/native

@bryanltobing
Copy link

Damn, am I understanding this correctly that there is no way to change the height of the header other than a completely custom from-scratch header component?

if this is the case.

Is there a guide on how to properly build a custom header? if i make my own implementation i always can't get it right with the transition, and the title

@KCErb
Copy link

KCErb commented Jul 26, 2023

In my case, I wrapped my app with "safe area insets" and found that my headers were too tall because they were including the status bar height.

So I tried using the Header component from the Elements library so that I could set the headerStatusBarHeight property: https://reactnavigation.org/docs/elements/#headerstatusbarheight.

header: () => <Header title="test" />,

and then I commented it out and found that the original header was now the correct size. This just seems like buggy behavior but maybe it's the bug you're looking for!

TL;DR

Try

yarn add @react-navigation/elements

and then use the Header component, even if you get rid of it later, merely importing the library seems to have this side effect.

@michaelkremenetsky
Copy link

Can this be reopened because of https://x.com/swmansion/status/1714324990491762873?s=46

@buddywood
Copy link

I had to use a custom Header component to get it to work.

@dgrcode
Copy link

dgrcode commented Dec 11, 2023

Try

yarn add @react-navigation/elements

and then use the Header component, even if you get rid of it later, merely importing the library seems to have this side effect.

I've tried this, but I lose the back button and any headerRight I had defined. Am I missing something?

@anyamiletic
Copy link

@dgrcode if you use a custom header component it actually replaces headerLeft, headerRight and headerTitle as a single component. Your UI will no longer render those and so you will have to render the whole header UI in your custom component

@timbtimbtimb
Copy link

I'd love to have control over the height too.

@sirusbaladi
Copy link

why is this closed?

@ameer2468
Copy link

Experiencing the same issue, need to pass a custom header component instead which is annoying.

@shadeemerhi
Copy link

Experiencing the same issue and it's extremely annoying

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests