react-navigation中StackNavigator+TabNavigator+DrawerNavigator嵌套的使用方式

1、導(dǎo)航欄分類
StackNavigator: 類似于普通的Navigator,實(shí)現(xiàn)不同的頁面進(jìn)行跳轉(zhuǎn)
TabNavigator: 相當(dāng)于里面的TabBarController,屏幕上方的標(biāo)簽欄,不同的tabs互相切換。
DrawerNavigator: 抽屜效果,側(cè)邊滑出

正常配置StackNavigator的時候,代碼如下

const Stack = StackNavigator({
    Main: { screen: Main },
    Detail: { screen: Detail }
},
{
    initialRouteName:'Main', // 默認(rèn)顯示界面 Index
    navigationOptions:  {
          headerTintColor: '#333',
          headerBackTitle: null,
         headerStyle: {
             backgroundColor: '#fff',
         },
        headerTitleStyle:{
             alignSelf:'center',
       },
   };
});

正常配置TabNavigator的時候,代碼如下

 const MainTab = TabNavigator({ 
     Home: {
        screen: Home,
        navigationOptions:{
            tabBarLabel: '首頁',
            tabBarIcon: ({tintColor}) => (
                <Image
                    source={{uri : '首頁'}}
                    style={[tabBarIcon, {tintColor: tintColor}]}
                />
            ),
        },
 },
 {
    tabBarPosition: 'bottom',//tab底部顯示
    swipeEnabled:false,// 禁止左右滑動
    animationEnabled:false,//禁止動畫
    tabBarOptions: {
       //TabBar樣式設(shè)置
        style: {
            height:50,
            backgroundColor:'white',// TabBar 背景色
        },
       //TabBar底部文字設(shè)置
        labelStyle: {
            fontSize: 12, 
            marginTop:-1,
        },
        //TabBar底部圖片設(shè)置
       tabBarIconStyle:{
            marginTop:-3,
        }
        activeTintColor:'#bb6b50',// 文字和圖片選中顏色
        inactiveTintColor:'#333',文字和圖片默認(rèn)顏色
        showLabel:false,
    }
 });

DrawerNavigator實(shí)現(xiàn)抽屜導(dǎo)航

const Drawer = DrawerNavigator({  
     Home: {
         screen: Home
     },  
 },
{
  drawerWidth:  300, // 展示的寬度
  drawerPosition: 'left', // 抽屜在左邊還是右邊
  contentComponent: CustomDrawerContentComponent // 自定義抽屜組件
}); 
const CustomDrawerContentComponent = props => {
      return (
          <View style={{flex:1}}>
               <TouchableOpacity style={styles.btnStyle}
                 onPress={() =>props.navigation.navigate("DrawerClose")}>
                 <Text>首頁</Text>
               </TouchableOpacity>
          </View>
     );
}

如果要實(shí)現(xiàn)StackNavigator嵌套TabNavigator+ DrawerNavigator,如圖效果


QQ20180522-210915-HD.gif

代碼如下

TabNavigator配置,看截圖


4E416639-09C8-4C2A-951B-CC6DFBC1C39A.png

56620971-9024-4A73-9616-7486449AF2DD.png

代碼如下

const MainScreentNavigator=TabNavigator({
    Home:{
        screen:Home,
        navigationOptions:{
           tabBarLabel:'首頁',
           headerTitle:'首頁',
           tabBarIcon:({tintColor,focused})=>(
                <View style={{height:22,width:22,backgroundColor:'red'}}/>
           ),
    }},
    Friend:{
        screen:Friend,
        navigationOptions:{
           tabBarLabel:'朋友',
           headerTitle:'朋友',
           tabBarIcon:({tintColor,focused})=>(
                <View style={{height:22,width:22,backgroundColor:'red'}}/>
           ),
    }},
    Find:{
        screen:Find,
        navigationOptions:{
           tabBarLabel:'好友',
           headerTitle:'好友',
           tabBarIcon:({tintColor,focused})=>(
                <View style={{height:22,width:22,backgroundColor:'red'}}/>
           ),
    }},
},
{
     tabBarPosition: 'bottom',
     swipeEnabled: false, // 禁止左右滑動
     animationEnabled:false,
     tabBarOptions: {
        activeTintColor: '#bb6b50', // 文字和圖片選中顏色
        inactiveTintColor: '#333', // 文字和圖片默認(rèn)顏色
        showIcon: true, // android 默認(rèn)不顯示 icon, 需要設(shè)置為 true 才會顯示
        indicatorStyle: {
            height: 0
        }, // android 中TabBar下面會顯示一條線,高度設(shè)為 0 后就不顯示線了
        style: {
            backgroundColor: 'white', // TabBar 背景色
            height:50,
        },
        labelStyle: {
            fontSize: 12, // 文字大小
            marginTop:-1,
        },
        tabBarIconStyle:{
            marginTop:-3,
        }
      }
   }
);

StackNavigator配置,看截圖


71B32CBB-B60C-4C74-B22C-47391A7942AA.png

代碼如下

const Stack= StackNavigator({
    Home:{
       screen:MainScreentNavigator,
    },
    FirstVC:{
       screen:FirstVC,
       navigationOptions:close
    },
    SecondVC:{
       screen:SecondVC,
       navigationOptions:close
    },
    ThirdVC:{
       screen:ThirdVC,
       navigationOptions:close
    },
    FourVC:{
       screen:FourVC,
       navigationOptions:close
    },
    FiveVC:{
      screen:FiveVC,
      navigationOptions:close
    }
}, {
   initialRouteName: 'Home',
   navigationOptions: {
        header: null //隱藏導(dǎo)航欄
   }
});

二級頁面不需要側(cè)拉出菜單需要加一個close
代碼是

const close = {
   //禁止打開菜單
   drawerLockMode: "locked-closed", 
   //允許使用返回手勢
   gesturesEnabled: true,
}
3D8581D9-4A52-41EF-AEFA-B03344F4557A.png

代碼如下

export default const Drawer = DrawerNavigator({
    Home: {
        screen: Stack,
    },
}, {
    drawerWidth:  width - 100, // 展示的寬度
    drawerPosition: 'left', // 抽屜在左邊還是右邊
    contentComponent: CustomDrawerContentComponent // 自定義抽屜組件
});

CustomDrawerContentComponent代碼
如下

const CustomDrawerContentComponent = props => {
  return (
    <ScrollView>
      <MyNav
        color={"#1AA1E5"}
        leftIconWidth={11}
        leftIconHeight={19}
        textColor={"white"}
      />
      <View
        style={{ backgroundColor: "white", height: height, width: width - 100 }}
      >
        <TouchableOpacity
          style={styles.btnStyle}
          onPress={() => props.navigation.navigate("DrawerClose")}
        >
          <Text>首頁</Text>
        </TouchableOpacity>
        <TouchableOpacity
          style={styles.btnStyle}
          onPress={() => props.navigation.navigate("FirstVC")}
        >
          <Text>錢包</Text>
        </TouchableOpacity>
        <TouchableOpacity
          style={styles.btnStyle}
          onPress={() => props.navigation.navigate("SecondVC")}
        >
          <Text>消息</Text>
        </TouchableOpacity>
        <TouchableOpacity
          style={styles.btnStyle}
          onPress={() => props.navigation.navigate("ThirdVC")}
        >
          <Text>redux</Text>
        </TouchableOpacity>
        <TouchableOpacity
          style={styles.btnStyle}
          onPress={() => props.navigation.navigate("FiveVC")}
        >
          <Text>商城</Text>
        </TouchableOpacity>
        <TouchableOpacity
          style={styles.btnStyle}
          onPress={() => props.navigation.navigate("FourVC")}
        >
          <Text>設(shè)置</Text>
        </TouchableOpacity>
         <TouchableOpacity
          style={styles.btnStyle}
          onPress={() => props.navigation.navigate("FourVC")}
        >
          <Text>退出登錄</Text>
        </TouchableOpacity>
      </View>
    </ScrollView>
  );
};

嵌套結(jié)構(gòu)就是這樣

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • react-navigation導(dǎo)航組件使用詳解 注意了,如果有小伙伴們發(fā)現(xiàn)運(yùn)行作者提供的react-naviga...
    光強(qiáng)_上海閱讀 23,656評論 38 103
  • 一、開源庫介紹 今年1月份,新開源的React-natvigation庫備受矚目。在短短不到3個月的時間,gith...
    德山_閱讀 2,361評論 0 19
  • DBUnit數(shù)據(jù)庫測試: DBUnit是一個基于junit擴(kuò)展的數(shù)據(jù)庫測試框架。它提供了大量的類對與數(shù)據(jù)庫相關(guān)的操...
    凱哥學(xué)堂閱讀 1,720評論 0 0
  • 楊柳青,麗人行, 美人養(yǎng)眼花怡情。 春風(fēng)拂花容。 桃花紅,杏花濃, 如花美眷游芳叢。 明年與誰同?
    tulipjia閱讀 283評論 0 7
  • 男性和女性生理上的構(gòu)造差異,造成天然的兩性關(guān)系的強(qiáng)弱勢。特別在特殊宗教、信息封閉的區(qū)域,無視女性意愿強(qiáng)行發(fā)生兩性關(guān)...
    weiyangteng閱讀 1,295評論 0 0

友情鏈接更多精彩內(nèi)容