将StackNavigator与TabNavigator集成

问题描述 投票:1回答:2

如何组合StackNavigator和TabNavigator?

我的以下代码有效:

index.android.js:

import React, { Component } from 'react';
import { AppRegistry, Text, View } from 'react-native';

import { StackNavigator,TabNavigator } from 'react-navigation';


import TestComp1 from './src/components/TestComp1'
import TestComp2 from './src/components/TestComp2'
import TestComp3 from './src/components/TestComp3'
import TestComp4 from './src/components/TestComp4'
import TestComp5 from './src/components/TestComp5'

export default class myApp extends Component {
  render() {
    return (

        <MyApp />

    );
  }
}


const MyApp = StackNavigator({
  TestComp1: {screen:TestComp1},
  TestComp2: {screen:TestComp2}
});

const Tabs = TabNavigator({
  TestComp3: {screen:TestComp3},
  TestComp4: {screen:TestComp4}
  TestComp5: {screen:TestComp5}
});

AppRegistry.registerComponent('myApp', () => myApp);

这仅适用于StackNavigator。我想保留现有导航并集成TabNavigation。如果我执行以下操作,现在在TestComp1

TestComp1:

import { StackNavigator, TabNavigator } from 'react-navigation';

import { FooterTabs } from './routes/FooterTabs';

export default class HomePage extends Component {

static navigationOptions = {
    header: null
  };

  render() {
  return(
    <View style={styles.container}>
      <View style={styles.mainContent}>

        <Button
              onPress={() => this.props.navigation.navigate('TestComp1', {name: 'Lucy'})}
              title="NewPage"
        />

        <FooterTabs />  //Page shows all StackNavigator screens if I add this

        </View>
      </View>
    )
  }

}

和FooterTabs:

import { StackNavigator, TabNavigator } from 'react-navigation';

import TestComp3 from '../TestComp3';
import TestComp4 from '../TestComp4';
import TestComp5 from '../TestComp5';



export const FooterTabs = TabNavigator({

  Tab1: {
    screen: TestComp3
  },
  Tab2: {
    screen: TestComp4
  },
  Tab3: {
    screen: TestComp5
  },

})

显示FooterTabs,但TestComp1TestComp2也显示在彼此之下。我该如何解决?谢谢。

更新1:

enter image description here

更新2:

const Tabs = TabNavigator({
  TestComp3: {screen:TestComp1},
  TestComp4: {
    screen:TestComp4,
    navigationOptions: ({ navigation }) => ({
        title: "TestComp4",
        tabBarIcon: ({ tintColor }) => <MaterialIcons name="accessibility" size={20}/>
      })

  }

更新3

我为DrawerNavigator添加了另一个const并将其配置为:

const Drawer = DrawerNavigator({

  First:{
    screen: DrawerScreen1
  },
  Second:{
    screen: DrawerScreen2
  }

},{
  initialRouteName:'First',
  drawerPosition: 'left'
});

并包含在应用程序中:

const MyApp = StackNavigator({
  TestComp1: {screen:TestComp1},
  TestComp2: {screen:TestComp2},
  Tabs: {
     screen: Tabs
  },
  Drawer: {
     screen: Drawer 
  },
}, {
   initialRouteName: "Tabs"
});

我在说它:

<Button
  onPress={() => this.props.navigation.navigate('DrawerOpen')}
  title="Show Drawer"
/>

此按钮的OnPress调用DrawerScreen1但作为组件,它不显示为左侧的抽屉。我错过了什么?

reactjs react-native react-navigation
2个回答
7
投票

你可以试试这个:

const Tabs = TabNavigator({
  TestComp3: {screen:TestComp3},
  TestComp4: {screen:TestComp4}
  TestComp5: {screen:TestComp5}
}); 

const MyApp = StackNavigator({
  TestComp1: {screen:TestComp1},
  TestComp2: {screen:TestComp2},
  Tabs: {
     screen: Tabs
  }
}, {
   initialRouteName: "Tabs"
});

并从<FooterTabs />中删除TestComp1


0
投票

我们现在看看。你需要的是首先一个StackNavigator然后在StackNavigator的一个屏幕内你需要一个TabNavigator。对?

答案如下:对于index.android.js:

import React, { Component } from 'react';
import { AppRegistry, Text, View } from 'react-native';

import { StackNavigator } from 'react-navigation';


import TestComp1 from './src/components/TestComp1'
import TestComp2 from './src/components/TestComp2'

// Don't need to export default here since this is the root component 
// that is registered with AppRegistry and we don't need to import it anywhere.

class myApp extends Component {
  render() {
    return (
        <MyApp />
    );
  }
}

// Notice that these two screens will consist the navigation stack.
// Assume, TestComp1 contains our Tabbed layout. 

const MyApp = StackNavigator({
  TestComp1: { screen:TestComp1 }, // This screen will have tabs.
  TestComp2: { screen:TestComp2 }
});

AppRegistry.registerComponent('myApp', () => myApp);

现在让我们转到你的TestComp1,这是有你标签的屏幕。

TestComp1:

// ... all imports here.

// This should be your first tab.

class Home extends Component {

  static navigationOptions = {
    // Label for your tab. Can also place a tab icon here.
    tabBarLabel: 'Home',
  };

  render() {
    return(
     <View style={styles.container}>
       <View style={styles.mainContent}>
         // This will change your tab to 'Profile'.
         <Button
               onPress={() => this.props.navigation.navigate('Profile', {name: 'Lucy'})}
               title="NewPage"
         />
         </View>
       </View>
     )
   }
 }

 // This can be your second tab and so on. 

 class Profile extends Component {

    static navigationOptions = {
    // Label for your tab.
    tabBarLabel: 'Profile',
  };

    render() {
       return (
         <Text>This is the profile Tab.<Text>
       );
    }
 }

 export default TabNavigator({
   Home: {
      screen: Home,
   },
   Profile: {
     screen: Profile,
   },
 }, { 

   // This will get the tabs to show their labels/icons at the bottom.
   tabBarPosition: 'bottom',

  animationEnabled: true,
  tabBarOptions: {
    activeTintColor: '#e91e63',
  },
});

所以基本上,你的TestComp1里面有两个组件(HomeProfile),它们都是TabNavigator的一部分,因此它们可以作为标签。 (你不需要担心单独的页脚组件,因为ReactNavigation会自动使用你的组件的navigationOptions)我们将导出这个TabNavigator实例,我们将其用作index.android.js的导入,我们将把这个导入放入我们的StackNavigator作为应用程序的屏幕。

这样你就可以在你的RN应用程序中加入Tabs以及StackNavigator。此外,tabBarPosition: 'bottom'将您的标签放在底部。您也可以不再维护单独的FooterTabs组件。

如果您需要更多清晰度或微调,请阅读docs

希望我帮了。 :)

© www.soinside.com 2019 - 2024. All rights reserved.