期待已久的新教程上線啦!解鎖React Native開發新姿式,一網打盡React Native最新與最熱技術,點我Get!!!html
createBottomTabNavigator
至關於iOS裏面的TabBarController,屏幕下方的標籤欄。如圖:react
createBottomTabNavigator(RouteConfigs, BottomTabNavigatorConfig):
ios
RouteConfigs
(必選):路由配置對象是從路由名稱到路由配置的映射,告訴導航器該路由呈現什麼。BottomTabNavigatorConfig
(可選):配置導航器的路由(如:默認首屏,navigationOptions,paths等)樣式(如,轉場模式mode、頭部模式等)。從createBottomTabNavigator API上能夠看出createBottomTabNavigator
支持經過RouteConfigs
和 BottomTabNavigatorConfig
兩個參數來建立createBottomTabNavigator導航器。react-native
RouteConfigs支持三個參數screen
、path
以及navigationOptions
;數組
screen
(必選):指定一個 React 組件做爲屏幕的主要顯示內容,當這個組件被TabNavigator加載時,它會被分配一個navigation
prop。path
(可選):用來設置支持schema跳轉時使用,具體使用會在下文的有關Schema
章節中講到;navigationOptions
(可選):用以配置全局的屏幕導航選項如:title、headerRight、headerLeft等;TabBarBottom
,在Android平臺上默認使用TabBarTop
。
TabBarBottom
與TabBarTop
都是react-navigation
所支持的組件,要自定義TabBar能夠重寫這兩個組件也能夠根據須要本身實現一個;eg:函數
tabBarOptions: {
labelStyle: {
fontSize: 12,
},
tabStyle: {
width: 100,
},
style: {
backgroundColor: 'blue',
},
}
複製代碼
createBottomTabNavigator支持的屏幕導航選項的參數有:測試
export const AppTabNavigator = createBottomTabNavigator({
Page1: {
screen: Page1,
navigationOptions: {
tabBarLabel: 'Page1',
tabBarIcon: ({tintColor, focused}) => (
<Ionicons
name={focused ? 'ios-home' : 'ios-home-outline'}
size={26}
style={{color: tintColor}}
/>
),
}
},
Page2: {
screen: Page2,
navigationOptions: {
tabBarLabel: 'Page2',
tabBarIcon: ({tintColor, focused}) => (
<Ionicons
name={focused ? 'ios-people' : 'ios-people-outline'}
size={26}
style={{color: tintColor}}
/>
),
}
},
Page3: {
screen: Page3,
navigationOptions: {
tabBarLabel: 'Page3',
tabBarIcon: ({tintColor, focused}) => (
<Ionicons
name={focused ? 'ios-chatboxes' : 'ios-chatboxes-outline'}
size={26}
style={{color: tintColor}}
/>
),
}
},
}, {
tabBarComponent: TabBarComponent,
tabBarOptions: {
activeTintColor: Platform.OS === 'ios' ? '#e91e63' : '#fff',
}
});
複製代碼
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="跳轉到頁面2"
onPress={() => {
navigation.navigate("Page3",{ name: 'Devio' })
}}
/>
<Button
title="Go Back"
onPress={() => {
navigation.goBack();
}}
/>
複製代碼
代碼解析:
頁面跳轉可分爲兩步:
const {navigation} = this.props;
複製代碼
navigate(routeName, params, action)
進行頁面跳轉:navigation.navigate('Page2');
navigation.navigate('Page3',{ name: 'Devio' });
複製代碼
這裏在跳轉到Page3
的時候傳遞了參數{ name: 'Devio' }
;
export default class Page1 extends React.Component {
//也可在這裏定義每一個頁面的導航屬性,這裏的定義會覆蓋掉別處的定義
// static navigationOptions = {
// title: 'Page1',
// };
render() {
const {navigation} = this.props;
return <View style={{flex: 1, backgroundColor: "gray",}}>
<Text style={styles.text}>歡迎來到Page1</Text>
<Button
title="Go Back"
onPress={() => {
navigation.goBack();
}}
/>
<Button
title="改變主題色"
onPress={() => {
navigation.setParams({theme:{
tintColor:'orange',
updateTime:new Date().getTime()
}})
}}
/>
<Button
title="跳轉到頁面2"
onPress={() => {
navigation.navigate("Page2")
}}
/>
</View>
}
}
複製代碼
代碼解析:
在上述代碼中經過:
<Button
title="改變主題色"
onPress={() => {
navigation.setParams({theme:{
tintColor:'orange',
updateTime:new Date().getTime()
}})
}}
/>
複製代碼
更新當前主題,你會看到當點擊「改變主題色「按鈕時,TabBar的顏色也會跟着改變。
當用戶單擊Go Back
按鈕時,經過:
navigation.goBack();
複製代碼
實現了返回到默認的Tab。
在使用react-navigation時每每有些需求經過簡單的配置是沒法完成的,好比:
相似上述的應用場景有不少,你們能夠經過與本教程配套的最新版React Native+Redux打造高質量上線App視頻教程進行進一步學習react-navigation的更多高級應用。