以筠 發表於 2019-9-1 15:28:00

React Native 之 createBottomTabNavigator,createMaterialTopTabNavigator

<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">icon第三方库
yarn add react</span>-native-vector-<span style="color: rgba(0, 0, 0, 1)">icons
react</span>-native link react-native-vector-icons </pre>
</div>
<p>在上次的代码中添加:</p>
<p>AppNavigators.js</p>
<div class="cnblogs_code"><img id="code_img_closed_5691c106-82d7-4873-a23f-d326d5b0aee5" class="code_img_closed" src="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" alt=""><img id="code_img_opened_5691c106-82d7-4873-a23f-d326d5b0aee5" class="code_img_opened" style="display: none" src="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" alt="">
<div id="cnblogs_code_open_5691c106-82d7-4873-a23f-d326d5b0aee5" class="cnblogs_code_hide">
<pre>import React from 'react'; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">只要在页面中使用了基础组件 都需要导入这句话 不然会报错</span>
import {Button,Platform} from 'react-native'<span style="color: rgba(0, 0, 0, 1)">;
import { createStackNavigator,createAppContainer,createBottomTabNavigator,createMaterialTopTabNavigator } from </span>'react-navigation'<span style="color: rgba(0, 0, 0, 1)">;
import HomePage from </span>'../pages/HomePage'<span style="color: rgba(0, 0, 0, 1)">;
import Page1 from </span>'../pages/Page1'<span style="color: rgba(0, 0, 0, 1)">;
import Page2 from </span>'../pages/Page2'<span style="color: rgba(0, 0, 0, 1)">;
import Page3 from </span>'../pages/Page3'<span style="color: rgba(0, 0, 0, 1)">;
import Page4 from </span>'../pages/Page4'<span style="color: rgba(0, 0, 0, 1)">;
import Page5 from </span>'../pages/Page5'<span style="color: rgba(0, 0, 0, 1)">;
import Ionicons from </span>'react-native-vector-icons/Ionicons'<span style="color: rgba(0, 0, 0, 1)">

const AppTopNavigator</span>=<span style="color: rgba(0, 0, 0, 1)">createMaterialTopTabNavigator(
{
Page1:{
   screen:Page1,
   navigationOptions:{
       tabBarLabel: </span>'All'<span style="color: rgba(0, 0, 0, 1)">
   }
},
Page2:{
   screen:Page2,
   navigationOptions:{
       tabBarLabel: </span>'iOS'<span style="color: rgba(0, 0, 0, 1)">
   }
},
Page3:{
   screen:Page3,
   navigationOptions:{
       tabBarLabel: </span>'Android'<span style="color: rgba(0, 0, 0, 1)">
   }
},
Page4:{
   screen:Page4,
   navigationOptions:{
       tabBarLabel: </span>'React-Native'<span style="color: rgba(0, 0, 0, 1)">
   }
},
},
{
tabBarOptions:{
    tabStyle:{mindWidth: </span>50<span style="color: rgba(0, 0, 0, 1)">},
    upperCaseLabel:</span><span style="color: rgba(0, 0, 255, 1)">false</span>,<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">是否使标签大写 默认true</span>
    scrollEndabled:<span style="color: rgba(0, 0, 255, 1)">true</span>,<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">是否支持选项卡滚动 默认false</span>
<span style="color: rgba(0, 0, 0, 1)">    style:{
      backgroundColor:</span>'#678'<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">TabBar背景色</span>
<span style="color: rgba(0, 0, 0, 1)">    },
    indicatorStyle:{
      height:</span>2<span style="color: rgba(0, 0, 0, 1)">,
      backgroundColor:</span>'white'<span style="color: rgba(0, 0, 0, 1)">
    },</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">标签指示器样式</span>
<span style="color: rgba(0, 0, 0, 1)">    labelStyle:{
      fontSize:</span>13<span style="color: rgba(0, 0, 0, 1)">,
      marginTop:</span>6<span style="color: rgba(0, 0, 0, 1)">,
      marginBottom:</span>6<span style="color: rgba(0, 0, 0, 1)">
    },</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 文字的样式</span>
<span style="color: rgba(0, 0, 0, 1)">

}
}
);


const AppBottomNavigator</span>=<span style="color: rgba(0, 0, 0, 1)">createBottomTabNavigator(
{
    Page1:{
       screen:Page1,
       navigationOptions:{
         tabBarLabel: </span>'最热'<span style="color: rgba(0, 0, 0, 1)">,
         tabBarIcon:({tintColor,focused})</span>=&gt;(&lt;<span style="color: rgba(0, 0, 0, 1)">Ionicons
            name</span>={'ios-home'<span style="color: rgba(0, 0, 0, 1)">}
            size</span>={26<span style="color: rgba(0, 0, 0, 1)">}
            style</span>=<span style="color: rgba(0, 0, 0, 1)">{{color:tintColor}}
         </span>/&gt;)
<span style="color: rgba(0, 0, 0, 1)">       }
    },
    Page2:{
       screen:Page2,
       navigationOptions:{
         tabBarLabel: </span>'趋势'<span style="color: rgba(0, 0, 0, 1)">,
         tabBarIcon:({tintColor,focused})</span>=&gt;(&lt;<span style="color: rgba(0, 0, 0, 1)">Ionicons
            name</span>={'ios-appstore'} <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 全部小写</span>
            size={26<span style="color: rgba(0, 0, 0, 1)">}
            style</span>=<span style="color: rgba(0, 0, 0, 1)">{{color:tintColor}}
         </span>/&gt;)
<span style="color: rgba(0, 0, 0, 1)">       }
    },
    Page3:{
       screen:Page3,
       navigationOptions:{
         tabBarLabel: </span>'收藏'<span style="color: rgba(0, 0, 0, 1)">,
         tabBarIcon:({tintColor,focused})</span>=&gt;(&lt;<span style="color: rgba(0, 0, 0, 1)">Ionicons
            name</span>={'ios-people'<span style="color: rgba(0, 0, 0, 1)">}
            size</span>={26<span style="color: rgba(0, 0, 0, 1)">}
            style</span>=<span style="color: rgba(0, 0, 0, 1)">{{color:tintColor}}
         </span>/&gt;)
<span style="color: rgba(0, 0, 0, 1)">       }
    },
    Page4:{
       screen:Page4,
       navigationOptions:{
         tabBarLabel: </span>'我的'<span style="color: rgba(0, 0, 0, 1)">,
         tabBarIcon:({tintColor,focused})</span>=&gt;(&lt;<span style="color: rgba(0, 0, 0, 1)">Ionicons
            name</span>={'ios-aperture'<span style="color: rgba(0, 0, 0, 1)">}
            size</span>={26<span style="color: rgba(0, 0, 0, 1)">}
            style</span>=<span style="color: rgba(0, 0, 0, 1)">{{color:tintColor}}
         </span>/&gt;)
<span style="color: rgba(0, 0, 0, 1)">       }
    },
},
{
    tabBarOptions:{
      activeTintColor: Platform.OS </span>=== 'ios' ? '#e91e63' : '#fff'<span style="color: rgba(0, 0, 0, 1)">,
    }
}
);


const AppStackNavigator </span>=<span style="color: rgba(0, 0, 0, 1)"> createStackNavigator({
    HomePage: {
      screen: HomePage
    },
    Page1: {
      screen: Page1,
      navigationOptions: ({navigation}) </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> ({
            title: `${navigation.state.params.name}页面名`</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">动态设置navigationOptions</span>
<span style="color: rgba(0, 0, 0, 1)">      })
    },
    Page2: {
      screen: Page2,
      navigationOptions: {</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">在这里定义每个页面的导航属性,静态配置</span>
            title: "This is Page2."<span style="color: rgba(0, 0, 0, 1)">,
      }
    },
    Page3: {
      screen: Page3,
      navigationOptions: (props) </span>=&gt; {<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">在这里定义每个页面的导航属性,动态配置</span>
            const {navigation} =<span style="color: rgba(0, 0, 0, 1)"> props;
            const {state, setParams} </span>=<span style="color: rgba(0, 0, 0, 1)"> navigation;
            const {params} </span>=<span style="color: rgba(0, 0, 0, 1)"> state;
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> {
                title: params.title </span>? params.title : 'This is Page3'<span style="color: rgba(0, 0, 0, 1)">,
                headerRight: (
                  </span>&lt;<span style="color: rgba(0, 0, 0, 1)">Button
                        title</span>={params.mode === 'edit' ? '保存' : '编辑'<span style="color: rgba(0, 0, 0, 1)">}
                        onPress</span>={()=&gt;{setParams({mode: params.mode === 'edit' ? '' : 'edit'<span style="color: rgba(0, 0, 0, 1)">})}
                            }
                  </span>/&gt;
<span style="color: rgba(0, 0, 0, 1)">                ),
            }
      }
    },

    Bottom:{
      screen:AppBottomNavigator,
      navigationOptions:{
      title:</span>'BottomNavigator'<span style="color: rgba(0, 0, 0, 1)">
      }
    },

    Top:{
      screen:AppTopNavigator,
      navigationOptions:{
      title:</span>'TopNavigator'<span style="color: rgba(0, 0, 0, 1)">
      }
    }

},
{
    defaultNavigationOptions: {
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> header: null,// 可以通过将header设为null 来禁用StackNavigator的Navigation Bar</span>
<span style="color: rgba(0, 0, 0, 1)">    }
}
);

const App </span>=<span style="color: rgba(0, 0, 0, 1)"> createAppContainer(AppStackNavigator)
export </span><span style="color: rgba(0, 0, 255, 1)">default</span> App</pre>
</div>
<span class="cnblogs_code_collapse">View Code</span></div>
<p>HomePage.js</p>
<div class="cnblogs_code"><img id="code_img_closed_646860aa-22df-4231-935a-39f031ade770" class="code_img_closed" src="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" alt=""><img id="code_img_opened_646860aa-22df-4231-935a-39f031ade770" class="code_img_opened" style="display: none" src="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" alt="">
<div id="cnblogs_code_open_646860aa-22df-4231-935a-39f031ade770" class="cnblogs_code_hide">
<pre><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
* @flow
</span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">

import React, {Fragment,Component} from </span>'react'<span style="color: rgba(0, 0, 0, 1)">;
import {
   StyleSheet,
   View,
   Text,
   Button,
} from </span>'react-native'<span style="color: rgba(0, 0, 0, 1)">;
type Props </span>=<span style="color: rgba(0, 0, 0, 1)"> {};
export </span><span style="color: rgba(0, 0, 255, 1)">default</span> class HomePage extends Component&lt;Props&gt;<span style="color: rgba(0, 0, 0, 1)"> {

   </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">修改Back按钮</span>
   static navigationOptions=<span style="color: rgba(0, 0, 0, 1)">{
   title:</span>'Home'<span style="color: rgba(0, 0, 0, 1)">,
   headerBackTitle:</span>'返回哈哈'<span style="color: rgba(0, 0, 0, 1)">
   }
   render(){
   const {navigation}</span>=<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.props;

   </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> (
       </span>&lt;View style={styles.container}&gt;
         &lt;Text style={styles.welcome}&gt;欢迎来到HomePage&lt;/Text&gt;

         &lt;<span style="color: rgba(0, 0, 0, 1)">Button
         title</span>={'去 Page1'<span style="color: rgba(0, 0, 0, 1)">}
         onPress</span>={()=&gt;<span style="color: rgba(0, 0, 0, 1)">{
             navigation.navigate(</span>'Page1',{name:'动态的'<span style="color: rgba(0, 0, 0, 1)">});
         }}
         </span>/&gt;

         &lt;<span style="color: rgba(0, 0, 0, 1)">Button
         title</span>={'去 Page2'<span style="color: rgba(0, 0, 0, 1)">}
         onPress</span>={()=&gt;<span style="color: rgba(0, 0, 0, 1)">{
             navigation.navigate(</span>'Page2'<span style="color: rgba(0, 0, 0, 1)">);
         }}
         </span>/&gt;

         &lt;<span style="color: rgba(0, 0, 0, 1)">Button
         title</span>={'去 Page3'<span style="color: rgba(0, 0, 0, 1)">}
         onPress</span>={()=&gt;<span style="color: rgba(0, 0, 0, 1)">{
             navigation.navigate(</span>'Page3',{name:'Dev iOS'<span style="color: rgba(0, 0, 0, 1)">});
         }}
         </span>/&gt;

         &lt;<span style="color: rgba(0, 0, 0, 1)">Button
         title</span>={'去 Bottom Navigator'<span style="color: rgba(0, 0, 0, 1)">}
         onPress</span>={()=&gt;<span style="color: rgba(0, 0, 0, 1)">{
             navigation.navigate(</span>'Bottom'<span style="color: rgba(0, 0, 0, 1)">);
         }}
         </span>/&gt;

         &lt;<span style="color: rgba(0, 0, 0, 1)">Button
         title</span>={'去 Top Navigator'<span style="color: rgba(0, 0, 0, 1)">}
         onPress</span>={()=&gt;<span style="color: rgba(0, 0, 0, 1)">{
             navigation.navigate(</span>'Top'<span style="color: rgba(0, 0, 0, 1)">);
         }}
         </span>/&gt;

       &lt;/View&gt;
<span style="color: rgba(0, 0, 0, 1)">       );
   }
}

const styles</span>=<span style="color: rgba(0, 0, 0, 1)">StyleSheet.create({
   container:{
   flex:</span>1<span style="color: rgba(0, 0, 0, 1)">,
   },
   welcome:{
   fontSize:</span>20<span style="color: rgba(0, 0, 0, 1)">,
   textAlign: </span>'center'<span style="color: rgba(0, 0, 0, 1)">,
   }

});</span></pre>
</div>
<span class="cnblogs_code_collapse">View Code</span></div>
<p>效果图</p>
<p><img src="https://img2018.cnblogs.com/blog/1025493/201909/1025493-20190901152753599-144393779.gif" alt="" width="361" height="782"></p>
<p>&nbsp;</p>
<p>https://zamarrowski.github.io/react-ionicons/&nbsp;图标网址</p>
<p>https://reactnavigation.org/docs/en/tab-based-navigation.html 导航栏指导文档</p>
<p>&nbsp;</p>
<p>--------------------------分界线--------------------------</p>
<p>如果AppNavigators.js做如下配置,那么每个Page都能拥有底部切换Bar了&nbsp;</p>
<div class="cnblogs_code">
<pre>import React from 'react'; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">只要在页面中使用了基础组件 都需要导入这句话 不然会报错</span>
import {Button} from 'react-native'<span style="color: rgba(0, 0, 0, 1)">;
import { createStackNavigator,createAppContainer,createBottomTabNavigator } from </span>'react-navigation'<span style="color: rgba(0, 0, 0, 1)">;
import HomePage from </span>'../pages/HomePage'<span style="color: rgba(0, 0, 0, 1)">;
import Page1 from </span>'../pages/Page1'<span style="color: rgba(0, 0, 0, 1)">;
import Page2 from </span>'../pages/Page2'<span style="color: rgba(0, 0, 0, 1)">;
import Page3 from </span>'../pages/Page3'<span style="color: rgba(0, 0, 0, 1)">;
import Page4 from </span>'../pages/Page4'<span style="color: rgba(0, 0, 0, 1)">;
import Page5 from </span>'../pages/Page5'<span style="color: rgba(0, 0, 0, 1)">;
import Ionicons from </span>'react-native-vector-icons/Ionicons'<span style="color: rgba(0, 0, 0, 1)">


const AppBottomNavigator</span>=<span style="color: rgba(0, 0, 0, 1)">createBottomTabNavigator({
Page1:{
       screen:Page1,
       navigationOptions:{
         tabBarLabel: </span>'最热'<span style="color: rgba(0, 0, 0, 1)">,
         tabBarIcon:({tintColor,focused})</span>=&gt;(&lt;<span style="color: rgba(0, 0, 0, 1)">Ionicons
            name</span>={'ios-home'<span style="color: rgba(0, 0, 0, 1)">}
            size</span>={26<span style="color: rgba(0, 0, 0, 1)">}
            style</span>=<span style="color: rgba(0, 0, 0, 1)">{{color:tintColor}}
         </span>/&gt;)
<span style="color: rgba(0, 0, 0, 1)">       }
    },
    Page2:{
       screen:Page2,
       navigationOptions:{
         tabBarLabel: </span>'趋势'<span style="color: rgba(0, 0, 0, 1)">,
         tabBarIcon:({tintColor,focused})</span>=&gt;(&lt;<span style="color: rgba(0, 0, 0, 1)">Ionicons
            name</span>={'ios-appstore'} <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 全部小写</span>
            size={26<span style="color: rgba(0, 0, 0, 1)">}
            style</span>=<span style="color: rgba(0, 0, 0, 1)">{{color:tintColor}}
         </span>/&gt;)
<span style="color: rgba(0, 0, 0, 1)">       }
    },
}

);

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">HomePage Page1 Page2 这些是页面名,到时候导航器就接收这个参数进行界面跳转</span>
const AppStackNavigator =<span style="color: rgba(0, 0, 0, 1)"> createStackNavigator({
    HomePage: {
      screen: HomePage
    },
    Page1: {
      screen: AppBottomNavigator,
      navigationOptions: ({navigation}) </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> ({
            title: `${navigation.state.params.name}页面名`</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">动态设置navigationOptions</span>
<span style="color: rgba(0, 0, 0, 1)">      })
    },
    Page2: {
      screen: AppBottomNavigator,
      navigationOptions: {</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">在这里定义每个页面的导航属性,静态配置</span>
            title: "This is Page2."<span style="color: rgba(0, 0, 0, 1)">,
      }
    },
    Page3: {
      screen: Page3,
      navigationOptions: (props) </span>=&gt; {<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">在这里定义每个页面的导航属性,动态配置</span>
            const {navigation} =<span style="color: rgba(0, 0, 0, 1)"> props;
            const {state, setParams} </span>=<span style="color: rgba(0, 0, 0, 1)"> navigation;
            const {params} </span>=<span style="color: rgba(0, 0, 0, 1)"> state;
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> {
                title: params.title </span>? params.title : 'This is Page3'<span style="color: rgba(0, 0, 0, 1)">,
                headerRight: (
                  </span>&lt;<span style="color: rgba(0, 0, 0, 1)">Button
                        title</span>={params.mode === 'edit' ? '保存' : '编辑'<span style="color: rgba(0, 0, 0, 1)">}
                        onPress</span>={()=&gt;{setParams({mode: params.mode === 'edit' ? '' : 'edit'<span style="color: rgba(0, 0, 0, 1)">})}
                            }
                  </span>/&gt;
<span style="color: rgba(0, 0, 0, 1)">                ),
            }
      }
    },
    Bottom:{
      screen:AppBottomNavigator,
      navigationOptions:{
      title:</span>'BottomNavigator'<span style="color: rgba(0, 0, 0, 1)">
      }
    },

}, {
    defaultNavigationOptions: {
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> header: null,// 可以通过将header设为null 来禁用StackNavigator的Navigation Bar</span>
<span style="color: rgba(0, 0, 0, 1)">    }
});


const App </span>=<span style="color: rgba(0, 0, 0, 1)"> createAppContainer(AppStackNavigator)
export </span><span style="color: rgba(0, 0, 255, 1)">default</span> App</pre>
</div>
<p>&nbsp;<img src="https://img2018.cnblogs.com/blog/1025493/201912/1025493-20191225114259770-298700953.png" alt="" width="538" height="498"></p>

</div>
<div id="MySignature" role="contentinfo">
    此文仅为鄙人学习笔记之用,朋友你来了,如有不明白或者建议又或者想给我指点一二,请私信我。liuw_flexi@163.com/QQ群:582039935.                        

我的gitHub: (学习代码都在gitHub)
https://github.com/nwgdegitHub/<br><br>
来源:https://www.cnblogs.com/liuw-flexi/p/11442451.html
頁: [1]
查看完整版本: React Native 之 createBottomTabNavigator,createMaterialTopTabNavigator