简体   繁体   English

在本机反应中删除后退按钮到兄弟堆栈

[英]Remove back button to sibling stack in react native

I am trying to remove the header (see image) on sibling navigators. 我试图删除兄弟导航器上的标题(见图)。

在此输入图像描述

I have a stackNavigator like so: 我有一个像这样的stackNavigator:

const navigator = createStackNavigator({
  'route': RouteComponent,
  'sibling1': Sibling1Navigator,
  'sibling2': Sibling2Navigator,
},
{
  ...defaultNavigationOptions,
  // @ts-ignore
  headerLayoutPreset: 'center',
  headerMode: 'screen',
})

sibling1Navigator looks like this: sibling1Navigator看起来像这样:

    const Sibling1Navigator = createStackNavigator(
  {
    'route1': Route1Component,
    'route1': Route2Component,
    'route3': Route3Component,
  },
  {
    transitionConfig: HorizontalSlideTransitionConfig,
    navigationOptions: ({ navigation: { goBack, state, navigate } }) => {
      return {
        headerTransparent: true,
        headerStyle: {
          backgroundColor: '#FFF0',
        },
        headerLeft: (
          // tslint:disable-next-line
          <Button />
        ),
      }
    },
  },
)

I use a header on route to show a title but on routes route1 or route2 I don't want the back to page (like image). 我用头route ,以显示标题,但对线路route1route2我不希望回到页面(如图像)。

I am using react-navigation: ^2.17.0 我正在使用react-navigation: ^2.17.0

I have seen lots of examples of how to do this. 我见过很多关于如何做到这一点的例子。 the simplest would be to add the config to each page. 最简单的方法是将配置添加到每个页面。 I have had a look at all the answers on this question similar question but I was hoping there was something I could do with the stackNavigators. 我已经看过这个问题的所有答案类似的问题,但我希望我能用stackNavigators做些什么。 Is there another way of doing it or does it have to be done inside of the component? 有没有其他方法可以做到这一点,还是必须在组件内完成?

You can hide the header by setting header style height and width to zero in React Navigation, 您可以通过在React Navigation中将标题样式高度和宽度设置为零来隐藏标题,

eg: 例如:

const SignInStack = createStackNavigator({
    sign: { screen: SignIn, 
        navigationOptions: {
            headerStyle: {
                height: 0,
                width: 0,
            },
        },
       },
});

or 要么

const SignInStack = createStackNavigator({
        sign: SignIn,
    }, 
    {
        navigationOptions: {
            headerStyle: {
               height: 0,
               width: 0,
            }
       }
    }
);

I worked this out by following the docs on from A tab navigator contains a stack and you want to hide the tab bar on specific screens 我按照文章导航器中的文档包含一个堆栈并且你想隐藏特定屏幕上的标签栏来解决这个问题

I split my navigators up as per the docs said is the recommended way of doing it. 我按照文档所说的那样将导航器分开,这是建议的方式。

Here is my new code to give the example of what I did. 这是我的新代码,以举例说明我的所作所为。

    const navigator = createStackNavigator({
  'route': RouteComponent,
},
{
  ...defaultNavigationOptions,
  // @ts-ignore
  headerLayoutPreset: 'center',
  headerMode: 'screen',
})

const ParentNavigator = createStackNavigator({
  'Another route': AnotherNavigator,
  'sibling1': Sibling1Navigator,
  'sibling2': Sibling2Navigator,
}, {
  headerMode: 'none',
  navigationOptions: ({ navigation: { goBack, state, navigate } }) => {
    return {
      tabBarVisible: false,
    }
  },
})

So in the parent stackNavigator I remove the header I can also remove tabBar as well. 所以在父stackNavigator中我删除了标题我也可以删除tabBar。

Hope this helps anyone looking for the same question I had. 希望这有助于任何人寻找我所拥有的相同问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM