react-navigation學(xué)習(xí)筆記四:createDrawerNavigator

搭建demo使用相關(guān)版本:
"react-native": "0.56.0",
"react-navigation": "^2.18.2"

介紹

打開一個側(cè)邊欄,抽屜效果。官網(wǎng)介紹

簡單使用 相關(guān)介紹都注釋在代碼里面了

//createDrawerNavigator
import React from 'react';
import {
    StyleSheet,
    View,
    Button,
    Text,
    Image
} from 'react-native';
import {createDrawerNavigator} from 'react-navigation';

class AppInfoScreen extends React.Component {
    static navigationOptions = {
        drawerLabel: 'appInfoPage',
        drawerIcon: ({tintColor}) => (
            <Image
                source={require('./image/homeH.png')}
                style={[styles.icon, {tintColor: tintColor}]}
            />
        ),
    };

    render() {
        return (
            <View style={styles.container}>
                <Text style={styles.text}>APP 信息展示頁</Text>
            </View>
        );
    }
}

class Setting extends React.Component {
    static navigationOptions = {
        drawerLabel: 'setting',
        drawerIcon: ({tintColor}) => (
            <Image
                source={require('./image/RecordH.png')}
                style={[styles.icon, {tintColor: tintColor}]}
            />
        ),
    };

    render() {
        return (
            <View style={styles.container}>
                <Button
                    style={styles.btn}
                    title={'側(cè)欄'}
                    onPress={() => {
                        this.props.navigation.openDrawer();
                    }}
                />
                <Text style={styles.text}>設(shè)置頁</Text>
            </View>
        );
    }
}

class MainScreen extends React.Component {
    
    //一直在想怎么樣實現(xiàn) 抽屜包裝一個組件 但是不要在側(cè)邊欄存在這個組件的相關(guān)顯示與響應(yīng)
    //于是在這里對drawerLabel給了一個View 并且將它隱藏了
    //運(yùn)行起來的結(jié)果看起來還不錯  沒有顯示  也沒有交互
    static navigationOptions = {
        drawerLabel: () => (
            <View style={{opacity: 0}}>
            </View>
        ),
    };

    render() {
        return (
            <View style={styles.container}>
                <Button
                    style={styles.btn}
                    title={'側(cè)欄'}
                    onPress={() => {
                        this.props.navigation.openDrawer();
                    }}
                />
                <Text style={styles.text}>首頁 進(jìn)行信息展示</Text>
            </View>
        );
    }
}


//this.props.navigation.openDrawer();//打開抽屜
//this.props.navigation.closeDrawer();//關(guān)閉抽屜
export default createDrawerNavigator({
    AppInfo: {
        screen: AppInfoScreen
    },
    Main: {
        screen: MainScreen
    },
    Set: {
        screen: Setting
    },
}, {
    order: ['AppInfo', 'Set', 'Main'],//routeNames數(shù)組,用于定義抽屜項目的順序。
    initialRouteName: 'Main',//初始路由的routeName。
    drawerLockMode: 'locked-open',//設(shè)置是否響應(yīng)手勢
    //'unlocked'   可以通過手勢和代碼 打開關(guān)閉抽屜
    //'locked-closed' 抽屜關(guān)閉狀態(tài)  不能通過手勢打開  只能通過代碼實現(xiàn)
    //'locked-open'  抽屜打開狀態(tài)  不能通過手勢關(guān)閉  只能通過代碼實現(xiàn)


    drawerWidth: 250, //抽屜的寬度或返回的功能。
    drawerPosition: 'left', //選項是left或right。默認(rèn)是left位置。
    useNativeAnimations: false, //啟用原生動畫。默認(rèn)是true。
    drawerBackgroundColor: 'pink', //使用抽屜背景獲取某種顏色。默認(rèn)是white。

    //用于呈現(xiàn)抽屜內(nèi)容的組件,例如導(dǎo)航項。收到navigation抽屜的道具。默認(rèn)為DrawerItems
    //用于自定義
    //contentComponent: '',


    //配置抽屜內(nèi)容  items相關(guān)
    contentOptions: {
        // items: [OtherScreen],//可以修改或覆蓋路由數(shù)組  不知道干嘛用的
        // activeItemKey: 'AppInfo', //識別活動路線的關(guān)鍵  也不知道干嘛用的

        activeTintColor: 'white', //活動標(biāo)簽的標(biāo)簽和圖標(biāo)顏色
        activeBackgroundColor: 'blue', //活動標(biāo)簽的背景顏色
        inactiveTintColor: 'black', //非活動標(biāo)簽的標(biāo)簽和圖標(biāo)顏色
        inactiveBackgroundColor: 'red', //非活動標(biāo)簽的背景顏色

        // //按下項目時要調(diào)用的函數(shù) 不知道是否使用錯誤 一直沒反應(yīng)
        //github上面有答案 在自定義視圖的時候 會有用
        // onItemPress(route) {
        //     console.log('onItemPress'+route);
        // },


        // itemsContainerStyle: '', //內(nèi)容部分的樣式對象
        // itemStyle: '', //單個項目的樣式對象,可以包含圖標(biāo)和 / 或標(biāo)簽
        // labelStyle: '', //Text當(dāng)標(biāo)簽是字符串時,樣式對象在內(nèi)容部分內(nèi)覆蓋樣式
        // activeLabelStyle: '', //Text當(dāng)標(biāo)簽是字符串(與之合并labelStyle)時,樣式對象覆蓋活動標(biāo)簽的樣式
        // inactiveLabelStyle: '', //Text當(dāng)標(biāo)簽是字符串(與之合并labelStyle)時,樣式對象覆蓋非活動標(biāo)簽的樣式
        // iconContainerStyle: '', //樣式對象以覆蓋View圖標(biāo)容器樣式。
    }

})
const styles = StyleSheet.create({
    container: {
        flex: 1,
        alignItems: 'center',
        justifyContent: 'center',
    },
    text: {
        color: 'red',
        backgroundColor: 'pink',
        fontSize: 15,
    },
    btn: {
        backgroundColor: 'red',
        color: 'blue',
        width: 60,
        height: 44,
        marginTop: 115,
        marginLeft: 100
    },
    icon: {
        width: 22,
        height: 22
    }
})

側(cè)邊欄的自定義 對比上面一段添加相關(guān)代碼

const CustomDrawerContentComponent = (props) => (
    <ScrollView>
        <SafeAreaView style={styles.container} forceInset={{ top: 'always', horizontal: 'never' }}>
            <View style={{width:200,height:100,backgroundColor:'red'}}></View>
        </SafeAreaView>
    </ScrollView>
);
export default createDrawerNavigator({
    AppInfo: {
        screen: AppInfoScreen
    },
    Main: {
        screen: MainScreen
    },
    Set: {
        screen: Setting
    },
}, {
    order: ['AppInfo', 'Set', 'Main'],//routeNames數(shù)組,用于定義抽屜項目的順序。
    initialRouteName: 'Main',//初始路由的routeName。
    drawerLockMode: 'unlocked',//設(shè)置是否響應(yīng)手勢
    //'unlocked'   可以通過手勢和代碼 打開關(guān)閉抽屜
    //'locked-closed' 抽屜關(guān)閉狀態(tài)  不能通過手勢打開  只能通過代碼實現(xiàn)
    //'locked-open'  抽屜打開狀態(tài)  不能通過手勢關(guān)閉  只能通過代碼實現(xiàn)


    drawerWidth: 250, //抽屜的寬度或返回的功能。
    drawerPosition: 'left', //選項是left或right。默認(rèn)是left位置。
    useNativeAnimations: false, //啟用原生動畫。默認(rèn)是true。
    drawerBackgroundColor: 'pink', //使用抽屜背景獲取某種顏色。默認(rèn)是white。

    //用于呈現(xiàn)抽屜內(nèi)容的組件,例如導(dǎo)航項。收到navigation抽屜的道具。默認(rèn)為DrawerItems
    //用于自定義
    contentComponent: CustomDrawerContentComponent,
})

API官網(wǎng)介紹

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1、通過CocoaPods安裝項目名稱項目信息 AFNetworking網(wǎng)絡(luò)請求組件 FMDB本地數(shù)據(jù)庫組件 SD...
    陽明AI閱讀 16,171評論 3 119
  • 擬古.問天 掩門癡立俟司晨, 索憶尋思亦杳昏。 雨夢殘聲三月盡, 風(fēng)竹肇事五更沉。 塵間只道親常倫, 路轉(zhuǎn)何能挽魄...
    微仲子孫閱讀 184評論 0 0
  • “除了故鄉(xiāng), 我只為一個人寫過月亮” 今天的月亮很圓 但 不大 不遠(yuǎn)處有一顆很亮的星 比月亮亮很多 走在路燈下 影...
    九里巷閱讀 238評論 0 0
  • 座了一天的車到了目地地,不容易啊。 早上沒到九點就準(zhǔn)備出發(fā)了,等朋友等到十點才上高速公路。一路高速,速度也不慢,還...
    馨之芬芳閱讀 265評論 0 0
  • 無論你身處世界的哪個角落,遠(yuǎn)在他鄉(xiāng)的你,走在陌生的人群中,若是碰巧聽到一句熟悉的家鄉(xiāng)話,心里也會涌上一股暖暖的親切...
    有趣的小王子閱讀 1,352評論 0 3

友情鏈接更多精彩內(nèi)容