官方推薦react-navigation的具體使用詳解
看了官方文檔的導(dǎo)航器對(duì)比,發(fā)現(xiàn)現(xiàn)在主推的方案是一個(gè)單獨(dú)的導(dǎo)航庫(kù)react-navigation,據(jù)說(shuō)它的使用十分簡(jiǎn)單。我就寫(xiě)個(gè)demo,試了一下。
一、主要構(gòu)成
按使用形式主要分三部分:
- StackNavigator: 類似于普通的Navigator,屏幕上方導(dǎo)航欄
- TabNavigator: 相當(dāng)于ios里面的TabBarController,屏幕下方的標(biāo)簽欄
- DrawerNavigator: 抽屜效果,側(cè)邊滑出
二、使用
1.新建項(xiàng)目
react-native init ComponentDemo
2. 在應(yīng)用中安裝此庫(kù)
npm install --save react-navigation
安裝完發(fā)現(xiàn)是beta版本(v1.0.0-beta.7) ,竟然有坑?!一會(huì)兒我們會(huì)詳細(xì)說(shuō)這個(gè)坑~
3.測(cè)試TabNavigator、StackNavigator和DrawerNavigator
(1)新建HomePage.js
import React from 'react';
import {
StyleSheet,
View,
Text,
Button,
Image
} from 'react-native';
import {
StackNavigator,
TabNavigator
} from 'react-navigation';
import ChatScreen from './ChatScreen';
import MinePage from './MinePage';
class HomePage extends React.Component{
static navigationOptions={
title: '首頁(yè)',//設(shè)置標(biāo)題內(nèi)容
header:{
backTitle: ' ',//返回按鈕標(biāo)題內(nèi)容(默認(rèn)為上一級(jí)標(biāo)題內(nèi)容)
}
}
constructor(props) {
super(props);
}
render() {
const {navigate} = this.props.navigation;
return (
<View style={styles.container}>
<Text style={{padding:10}}>Hello, Navigation!</Text>
<Button
onPress={() => navigate('Chat',{user:'Sybil'})}
title="點(diǎn)擊跳轉(zhuǎn)"/>
</View>
)
}
}
const MainScreenNavigator = TabNavigator({
Home: {
screen: HomePage,
navigationOptions: {
tabBar: {
label: '首頁(yè)',
icon: ({tintColor}) => (
<Image
source={require('./image/bar_home_nomarl@3x.png')}
style={[{tintColor: tintColor},styles.icon]}
/>
),
},
}
},
Certificate: {
screen: MinePage,
navigationOptions: {
tabBar: {
label: '我的',
icon: ({tintColor}) => (
<Image
source={require('./image/bar_center_normal@3x.png')}
style={[{tintColor: tintColor},styles.icon]}
/>
),
},
}
},
}, {
animationEnabled: false, // 切換頁(yè)面時(shí)不顯示動(dòng)畫(huà)
tabBarPosition: 'bottom', // 顯示在底端,android 默認(rèn)是顯示在頁(yè)面頂端的
swipeEnabled: false, // 禁止左右滑動(dòng)
backBehavior: 'none', // 按 back 鍵是否跳轉(zhuǎn)到第一個(gè) Tab, none 為不跳轉(zhuǎn)
tabBarOptions: {
activeTintColor: '#008AC9', // 文字和圖片選中顏色
inactiveTintColor: '#999', // 文字和圖片默認(rèn)顏色
showIcon: true, // android 默認(rèn)不顯示 icon, 需要設(shè)置為 true 才會(huì)顯示
indicatorStyle: {height: 0}, // android 中TabBar下面會(huì)顯示一條線,高度設(shè)為 0 后就不顯示線了
style: {
backgroundColor: '#fff', // TabBar 背景色
},
labelStyle: {
fontSize: 12, // 文字大小
},
},
});
const styles = StyleSheet.create({
container:{
flex: 1,
backgroundColor:'#fff'
},
icon: {
height: 22,
width: 22,
resizeMode: 'contain'
}
});
const SimpleApp = StackNavigator({
Home: {screen: MainScreenNavigator},
Chat:{screen:ChatScreen},
});
export default SimpleApp;
(2)新建ChatScreen.js
import React from 'react';
import {
Button,
Image,
View,
Text
} from 'react-native';
class ChatScreen extends React.Component {
static navigationOptions = {
title:'聊天',
};
render() {
const {params} = this.props.navigation.state;
return (
<View style={{backgroundColor:'#fff',flex:1}}>
<Text style={{padding:20}}>Chat with {params.user}</Text>
</View>
);
}
}
export default ChatScreen;
(3)新建MinePage.js
import React,{Component} from 'react';
import {
Button,
Image,
View,
Text,
StyleSheet
} from 'react-native';
import {
DrawerNavigator
} from 'react-navigation';
import MyNotificationsScreen from './MyNotificationsScreen';
class MinePage extends Component{
static navigationOptions = {
title:'我的',
drawerLabel: '我的',
// Note: By default the icon is only shown on iOS. Search the showIcon option below.
drawerIcon: ({ tintColor }) => (
<Image
source={require('./image/chat@3x.png')}
style={[styles.icon, {tintColor: tintColor}]}
/>
),
};
render(){;
return(
<View style={{backgroundColor:'#fff',flex:1}}>
<Text style={{padding:20}}>Sybil</Text>
<Button
style={{padding:20}}
onPress={() => this.props.navigation.navigate('DrawerOpen')}
title="點(diǎn)擊打開(kāi)側(cè)滑菜單"
/>
</View>
);
}
}
const styles = StyleSheet.create({
icon: {
width: 24,
height: 24,
},
});
const MyChatNavigator = DrawerNavigator({
MyChat: {
screen: MinePage,
},
Notifications: {
screen: MyNotificationsScreen,
},
},{
drawerWidth: 220, // 抽屜寬
drawerPosition: 'left', // 抽屜在左邊還是右邊
// contentComponent: CustomDrawerContentComponent, // 自定義抽屜組件
contentOptions: {
initialRouteName: MinePage, // 默認(rèn)頁(yè)面組件
activeTintColor: '#008AC9', // 選中文字顏色
activeBackgroundColor: '#f5f5f5', // 選中背景顏色
inactiveTintColor: '#000', // 未選中文字顏色
inactiveBackgroundColor: '#fff', // 未選中背景顏色
style: { // 樣式
}
}
});
export default MyChatNavigator;
(4)編寫(xiě)MyNotificationsScreen.js
import React from 'react';
import {
StyleSheet,
View,
Text,
Button,
Image
} from 'react-native';
class MyNotificationsScreen extends React.Component {
static navigationOptions = {
title:'通知',
drawerLabel: '通知',
drawerIcon: ({ tintColor }) => (
<Image
source={require('./image/notif@3x.png')}
style={[styles.tabIcon, {tintColor: tintColor}]}
/>
),
};
render() {
return (
<View style={{backgroundColor:'#fff'}}>
<Button
style={{padding:20}}
onPress={() => this.props.navigation.navigate('DrawerOpen')}
title="點(diǎn)擊打開(kāi)側(cè)滑菜單"
/>
<Button
onPress={() => this.props.navigation.goBack()}
title="返回我的界面"
/>
</View>
);
}
}
const styles = StyleSheet.create({
tabIcon: {
width: 24,
height: 24,
},
});
export default MyNotificationsScreen;
(5)運(yùn)行
報(bào)錯(cuò)啦?這就是上面我們所說(shuō)的坑~
什么原因呢?原來(lái)是測(cè)試版的bug,在目錄中找到node_modules/react-navigation/src/views/Header.js的第12行,刪除它就OK了~
Ps:遺憾的是這個(gè)錯(cuò)誤我沒(méi)有留圖啊~在我即將發(fā)表這篇文章的時(shí)候,最新版已經(jīng)變?yōu)?v1.0.0-beta.9)了,最新版已經(jīng)將上述的bug修改了!
好了,再次運(yùn)行~
上一個(gè)動(dòng)態(tài)效果圖:

想詳細(xì)了解React Navigation,可以閱讀這一篇英文的入門(mén)文檔,希望對(duì)你們有所幫助~
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
React實(shí)現(xiàn)動(dòng)態(tài)輪播圖的使用示例
輪播組件是常見(jiàn)的一種方式,用來(lái)展示圖像、信息或者是廣告,本文就來(lái)介紹一下React實(shí)現(xiàn)動(dòng)態(tài)輪播圖的使用示例,具有一定的參考價(jià)值,感興趣的可以了解一下2023-12-12
react native帶索引的城市列表組件的實(shí)例代碼
本篇文章主要介紹了react-native城市列表組件的實(shí)例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08
React中映射一個(gè)嵌套數(shù)組實(shí)現(xiàn)demo
這篇文章主要為大家介紹了React中映射一個(gè)嵌套數(shù)組實(shí)現(xiàn)demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
聊聊React onClick 傳遞參數(shù)的問(wèn)題
很多朋友向小編反映一個(gè)問(wèn)題關(guān)于React onClick 傳遞參數(shù)的問(wèn)題,當(dāng)點(diǎn)擊刪除按鈕需要執(zhí)行刪除操作,針對(duì)這個(gè)問(wèn)題該如何處理呢?下面小編給大家?guī)?lái)了React onClick 傳遞參數(shù)的問(wèn)題,感興趣的朋友一起看看吧2021-10-10
D3.js(v3)+react 實(shí)現(xiàn)帶坐標(biāo)與比例尺的散點(diǎn)圖 (V3版本)
散點(diǎn)圖(Scatter Chart),通常是一橫一豎兩個(gè)坐標(biāo)軸,數(shù)據(jù)是一組二維坐標(biāo),分別對(duì)應(yīng)兩個(gè)坐標(biāo)軸,與坐標(biāo)軸對(duì)應(yīng)的地方打上點(diǎn)。由此可以猜到,需要的元素包括circle(圓)和axis(坐標(biāo)軸),接下來(lái)通過(guò)本文大家分享D3.js(v3)+react 實(shí)現(xiàn)帶坐標(biāo)與比例尺的散點(diǎn)圖 (V3版本) ,一起看看2019-05-05
React中的權(quán)限組件設(shè)計(jì)問(wèn)題小結(jié)
這篇文章主要介紹了React中的權(quán)限組件設(shè)計(jì),整個(gè)過(guò)程也是遇到了很多問(wèn)題,本文主要來(lái)做一下此次改造工作的總結(jié),對(duì)React權(quán)限組件相關(guān)知識(shí)感興趣的朋友一起看看吧2022-07-07
解決React初始化加載組件會(huì)渲染兩次的問(wèn)題
這篇文章主要介紹了解決React初始化加載組件會(huì)渲染兩次的問(wèn)題,文中有出現(xiàn)這種現(xiàn)象的原因及解決方法,感興趣的同學(xué)跟著小編一起來(lái)看看吧2023-08-08
React函數(shù)組件與類組件使用及優(yōu)劣對(duì)比
本文主要介紹了React函數(shù)組件與類組件使用及優(yōu)劣對(duì)比,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04

