React Native高手进阶React Native学习

『React Navigation 3x系列教程』createD

2019-01-22  本文已影响23人  CrazyCodeBoy

这篇文章将向大家分享createDrawerNavigator的一些开发指南和实用技巧。

createDrawerNavigator抽屉效果,侧边滑出:

createDrawerNavigator.png

createDrawerNavigator API

createDrawerNavigator(RouteConfigs, DrawerNavigatorConfig):

从createDrawerNavigator API上可以看出createDrawerNavigator支持通过RouteConfigsDrawerNavigatorConfig两个参数来创建createDrawerNavigator导航器。

RouteConfigs

RouteConfigs支持三个参数screenpath以及navigationOptions

DrawerNavigatorConfig

自定义侧边栏(contentComponent)

DrawerNavigator有个默认的带滚动的侧边栏,你也可以通过重写这个侧边栏组件来自定义侧边栏:

contentComponent:(props) => (
    <ScrollView style={{backgroundColor:'#987656',flex:1}}>
        <SafeAreaView forceInset={{ top: 'always', horizontal: 'never' }}>
            <DrawerItems {...props} />
        </SafeAreaView>
    </ScrollView>
)

DrawerItems的contentOptions

contentOptions主要配置侧滑栏item的属性,只对DrawerItems,例如我们刚才写的例子,就可以通过这个属性来配置颜色,背景色等。其主要属性有:

eg:

contentOptions: {
  activeTintColor: '#e91e63',
  itemsContainerStyle: {
    marginVertical: 0,
  },
  iconContainerStyle: {
    opacity: 1
  }
}

提示:和本文配套的还有一个React Navigation3x的视频教程,欢迎学习。

navigationOptions(屏幕导航选项)

DrawerNavigator支持的屏幕导航选项的参数有:

侧边栏操作(打开、关闭、切换)

侧边栏支持以下几种操作方式:

navigation.openDrawer();
navigation.closeDrawer();
navigation.toggleDrawer();
//或
navigation.dispatch(DrawerActions.openDrawer());
navigation.dispatch(DrawerActions.closeDrawer());
navigation.dispatch(DrawerActions.toggleDrawer());

提示:和本文配套的还有一个React Navigation3x的视频教程,欢迎学习。

其中路由名openDrawer对应这打开侧边栏的操作,DrawerClose对应关闭侧边栏的操作,toggleDrawer对应切换侧边栏操作,要进行这些操作我么还需要一个navigationnavigation可以从props中获取;

其他API

【案例1】使用DrawerNavigator做界面导航、配置navigationOptions、自定义侧边栏

createDrawerNavigator

第一步:创建一个createDrawerNavigator类型的导航器

export const DrawerNav = createDrawerNavigator({
        Page4: {
            screen: Page4,
            navigationOptions: {
                drawerLabel: 'Page4',
                drawerIcon: ({tintColor}) => (
                    <MaterialIcons name="drafts" size={24} style={{color: tintColor}}/>
                ),
            }
        },
        Page5: {
            screen: Page5,
            navigationOptions: {
                drawerLabel: 'Page5',
                drawerIcon: ({tintColor}) => (
                    <MaterialIcons
                        name="move-to-inbox"
                        size={24}
                        style={{color: tintColor}}
                    />
                ),
            }
        },
    },
    {
        initialRouteName: 'Page4',
        contentOptions: {
            activeTintColor: '#e91e63',
        },
        contentComponent:(props) => (
            <ScrollView style={{backgroundColor:'#987656',flex:1}}>
                <SafeAreaView forceInset={{ top: 'always', horizontal: 'never' }}>
                    <DrawerItems {...props} />
                </SafeAreaView>
            </ScrollView>
        )
    }
);

提示:和本文配套的还有一个React Navigation3x的视频教程,欢迎学习。

第二步:配置navigationOptions:

DrawerNavigator的navigationOptions有两个关键的属性,tabBarLabel标签与tabBarIcon图标:

Page4: {
    screen: Page4,
    navigationOptions: {
        drawerLabel: 'Page4',
        drawerIcon: ({tintColor}) => (
            <MaterialIcons name="drafts" size={24} style={{color: tintColor}}/>
        ),
    }
},

提示:和本文配套的还有一个React Navigation3x的视频教程,欢迎学习。

在上述代码中使用了react-native-vector-icons的矢量图标作为Tab的显示图标,drawerIcon接收一个React 组件,大家可以根据需要进行定制:

第三步:界面跳转

render() {
    const {navigation} = this.props;
    return <View style={{flex: 1, backgroundColor: "#f67888",}}>
        <Text style={styles.text}>欢迎来到Page5</Text>
        <Button
            onPress={() => navigation.openDrawer()}
            title="Open drawer"
        />
        <Button
            onPress={() => navigation.toggleDrawer()}
            title="Toggle drawer"
        />
        <Button
            onPress={() => navigation.navigate('Page4')}
            title="Go to Page4"
        />
    </View>
}

代码解析:

页面跳转可分为两步:

     navigation.navigate('Page5');
 });

自定义侧边栏

如果DrawerNavigator的侧边栏的效果无法满足我们的需求,我们可以通过contentComponent属性来自定义侧边栏:

contentComponent:(props) => (
    <ScrollView style={{backgroundColor:'#987656',flex:1}}>
        <SafeAreaView forceInset={{ top: 'always', horizontal: 'never' }}>
            <DrawerItems {...props} />
        </SafeAreaView>
    </ScrollView>
)
上一篇 下一篇

猜你喜欢

热点阅读