官方推薦react-navigation的使用

看了官方文檔的導航器對比,發(fā)現(xiàn)現(xiàn)在主推的方案是一個單獨的導航庫react-navigation镊辕,據(jù)說它的使用十分簡單油够。我就寫個demo,試了一下征懈。

一石咬、主要構成

按使用形式主要分三部分:
1 StackNavigator: 類似于普通的Navigator,屏幕上方導航欄
2 TabNavigator: 相當于ios里面的TabBarController卖哎,屏幕下方的標簽欄
3 DrawerNavigator: 抽屜效果鬼悠,側邊滑出

二、使用

1.新建項目

react-native init ComponentDemo

2. 在應用中安裝此庫

npm install --save react-navigation
安裝完發(fā)現(xiàn)是beta版本(v1.0.0-beta.7) 亏娜,竟然有坑?焕窝!一會兒我們會詳細說這個坑~

3.測試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: '首頁',//設置標題內容
        header:{
            backTitle: ' ',//返回按鈕標題內容(默認為上一級標題內容)
        }
    }

    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="點擊跳轉"/>
            </View>
        )
    }
}
const MainScreenNavigator = TabNavigator({
    Home: {
        screen: HomePage,
        navigationOptions: {
            tabBar: {
                label: '首頁',
                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, // 切換頁面時不顯示動畫
    tabBarPosition: 'bottom', // 顯示在底端维贺,android 默認是顯示在頁面頂端的
    swipeEnabled: false, // 禁止左右滑動
    backBehavior: 'none', // 按 back 鍵是否跳轉到第一個 Tab它掂, none 為不跳轉
    tabBarOptions: {
        activeTintColor: '#008AC9', // 文字和圖片選中顏色
        inactiveTintColor: '#999', // 文字和圖片默認顏色
        showIcon: true, // android 默認不顯示 icon, 需要設置為 true 才會顯示
        indicatorStyle: {height: 0}, // android 中TabBar下面會顯示一條線,高度設為 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="點擊打開側滑菜單"
                />
            </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, // 默認頁面組件
        activeTintColor: '#008AC9',  // 選中文字顏色
        activeBackgroundColor: '#f5f5f5', // 選中背景顏色
        inactiveTintColor: '#000',  // 未選中文字顏色
        inactiveBackgroundColor: '#fff', // 未選中背景顏色
        style: {  // 樣式

        }
    }
});

export default MyChatNavigator;

(4)編寫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="點擊打開側滑菜單"
                />
                <Button
                    onPress={() => this.props.navigation.goBack()}
                    title="返回我的界面"
                />
            </View>
        );
    }
}

const styles = StyleSheet.create({
    tabIcon: {
        width: 24,
        height: 24,
    },
});

export default MyNotificationsScreen;

(5)運行
報錯啦溯泣?這就是上面我們所說的坑~
什么原因呢虐秋?原來是測試版的bug,在目錄中找到node_modules/react-navigation/src/views/Header.js的第12行垃沦,刪除它就OK了~

Ps:遺憾的是這個錯誤我沒有留圖啊~在我即將發(fā)表這篇文章的時候客给,最新版已經變?yōu)?v1.0.0-beta.9)了,最新版已經將上述的bug修改了肢簿!

好了靶剑,再次運行~
上一個動態(tài)效果圖:

QQ20170427-155140.gif

想詳細了解React Navigation,可以閱讀這一篇英文的入門文檔译仗,希望對你們有所幫助~

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末抬虽,一起剝皮案震驚了整個濱河市官觅,隨后出現(xiàn)的幾起案子纵菌,更是在濱河造成了極大的恐慌,老刑警劉巖休涤,帶你破解...
    沈念sama閱讀 212,718評論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件咱圆,死亡現(xiàn)場離奇詭異笛辟,居然都是意外死亡,警方通過查閱死者的電腦和手機序苏,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,683評論 3 385
  • 文/潘曉璐 我一進店門手幢,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人忱详,你說我怎么就攤上這事围来。” “怎么了匈睁?”我有些...
    開封第一講書人閱讀 158,207評論 0 348
  • 文/不壞的土叔 我叫張陵监透,是天一觀的道長。 經常有香客問我航唆,道長胀蛮,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,755評論 1 284
  • 正文 為了忘掉前任糯钙,我火速辦了婚禮粪狼,結果婚禮上,老公的妹妹穿的比我還像新娘任岸。我一直安慰自己再榄,他們只是感情好,可當我...
    茶點故事閱讀 65,862評論 6 386
  • 文/花漫 我一把揭開白布享潜。 她就那樣靜靜地躺著不跟,像睡著了一般。 火紅的嫁衣襯著肌膚如雪米碰。 梳的紋絲不亂的頭發(fā)上窝革,一...
    開封第一講書人閱讀 50,050評論 1 291
  • 那天,我揣著相機與錄音吕座,去河邊找鬼虐译。 笑死,一個胖子當著我的面吹牛吴趴,可吹牛的內容都是我干的漆诽。 我是一名探鬼主播,決...
    沈念sama閱讀 39,136評論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼锣枝,長吁一口氣:“原來是場噩夢啊……” “哼厢拭!你這毒婦竟也來了?” 一聲冷哼從身側響起撇叁,我...
    開封第一講書人閱讀 37,882評論 0 268
  • 序言:老撾萬榮一對情侶失蹤供鸠,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后陨闹,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體楞捂,經...
    沈念sama閱讀 44,330評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡薄坏,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 36,651評論 2 327
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了寨闹。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片胶坠。...
    茶點故事閱讀 38,789評論 1 341
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖繁堡,靈堂內的尸體忽然破棺而出沈善,到底是詐尸還是另有隱情,我是刑警寧澤椭蹄,帶...
    沈念sama閱讀 34,477評論 4 333
  • 正文 年R本政府宣布矮瘟,位于F島的核電站,受9級特大地震影響塑娇,放射性物質發(fā)生泄漏澈侠。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 40,135評論 3 317
  • 文/蒙蒙 一埋酬、第九天 我趴在偏房一處隱蔽的房頂上張望哨啃。 院中可真熱鬧,春花似錦写妥、人聲如沸拳球。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,864評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽祝峻。三九已至,卻和暖如春扎筒,著一層夾襖步出監(jiān)牢的瞬間莱找,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,099評論 1 267
  • 我被黑心中介騙來泰國打工嗜桌, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留奥溺,地道東北人。 一個月前我還...
    沈念sama閱讀 46,598評論 2 362
  • 正文 我出身青樓骨宠,卻偏偏與公主長得像浮定,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子层亿,可洞房花燭夜當晚...
    茶點故事閱讀 43,697評論 2 351

推薦閱讀更多精彩內容