這篇文章將向你們分享createMaterialTopTabNavigator的一些開發指南和實用技巧。html
createMaterialTopTabNavigator(RouteConfigs, TabNavigatorConfig):
react
RouteConfigs
(必選):路由配置對象是從路由名稱到路由配置的映射,告訴導航器該路由呈現什麼。TabNavigatorConfig
(可選):配置導航器的路由(如:默認首屏,navigationOptions,paths等)樣式(如,轉場模式mode、頭部模式等)。從createMaterialTopTabNavigator API上能夠看出createMaterialTopTabNavigator
支持經過RouteConfigs
和 TabNavigatorConfig
兩個參數來建立createMaterialTopTabNavigator導航器。ios
RouteConfigs支持三個參數screen
、path
以及navigationOptions
;react-native
screen
(必選):指定一個 React 組件做爲屏幕的主要顯示內容,當這個組件被TabNavigator加載時,它會被分配一個navigation
prop。path
(可選):用來設置支持schema跳轉時使用,具體使用會在下文的有關Schema
章節中講到;navigationOptions
(可選):用以配置全局的屏幕導航選項如:title、headerRight、headerLeft等;eg:數組
tabBarOptions: {
labelStyle: {
fontSize: 12,
},
tabStyle: {
width: 100,
},
style: {
backgroundColor: 'blue',
},
}
複製代碼
createMaterialTopTabNavigator支持的屏幕導航選項的參數有:bash
export const MaterialTopTabNavigator = createMaterialTopTabNavigator({//在這裏配置頁面的路由
Page1: {
screen: Page1,
navigationOptions: {
tabBarLabel: 'Page10',
tabBarIcon: ({tintColor, focused}) => (
<Ionicons
name={'ios-home'}
size={26}
style={{color: tintColor}}
/>
),
}
},
Page4: {
screen: Page4,
navigationOptions: {
tabBarLabel: 'Page2',
tabBarIcon: ({tintColor, focused}) => (
<Ionicons
name={'ios-people'}
size={26}
style={{color: tintColor}}
/>
),
}
},
Page3: {
screen: Page3,
navigationOptions: {
tabBarLabel: 'Page3',
tabBarIcon: ({tintColor, focused}) => (
<Ionicons
name={'ios-chatboxes'}
size={26}
style={{color: tintColor}}
/>
),
}
},
},
{
tabBarOptions: {
tabStyle: {
minWidth: 50
},
upperCaseLabel: false,//是否使標籤大寫,默認爲true
scrollEnabled: true,//是否支持 選項卡滾動,默認false
// activeTintColor: 'white',//label和icon的前景色 活躍狀態下(選中)
// inactiveTintColor: 'gray',//label和icon的前景色 活躍狀態下(未選中)
style: {
backgroundColor: '#678',//TabBar 的背景顏色
},
indicatorStyle: {
height: 2,
backgroundColor: 'white',
},//標籤指示器的樣式
labelStyle: {
fontSize: 13,
marginTop: 6,
marginBottom: 6,
},//文字的樣式
},
}
)
複製代碼
TabNavigator的navigationOptions有兩個關鍵的屬性,tabBarLabel標籤與tabBarIcon圖標:函數
Page2: {
screen: Page2,
navigationOptions: {
tabBarLabel: 'Page2',
tabBarIcon: ({tintColor, focused}) => (
<Ionicons
name={focused ? 'ios-people' : 'ios-people-outline'}
size={26}
style={{color: tintColor}}
/>
),
}
},
複製代碼
在上述代碼中使用了react-native-vector-icons
的矢量圖標做爲Tab的顯示圖標,tabBarIcon接收一個React 組件,你們能夠根據須要進行定製:學習
const {navigation} = this.props;
...
<Button
title="跳轉到頁面4"
onPress={() => {
navigation.navigate("Page4",{ name: 'Devio' })
}}
/>
複製代碼
代碼解析:測試
頁面跳轉可分爲兩步:字體
const {navigation} = this.props;
複製代碼
navigate(routeName, params, action)
進行頁面跳轉:navigation.navigate('Page2');
navigation.navigate('Page3',{ name: 'Devio' });
複製代碼
這裏在跳轉到Page3
的時候傳遞了參數{ name: 'Devio' }
;
在使用react-navigation時每每有些需求經過簡單的配置是沒法完成的,好比:
相似上述的應用場景有不少,你們能夠經過與本教程配套的最新版React Native+Redux打造高質量上線App視頻教程進行進一步學習react-navigation的更多高級應用。