一篇边学边总结react-navigation的文章

*该文章用于以后自己查看,所以文章逻辑不是很统一。

StackNavigator的基本流程

注册页面:

这里第一个注册的是顶层组件,会默认一开始显示。

import { StackNavigator } from 'react-navigation';
import Home from './src/container/Home/Home';
import Detail from './src/container/Detail/Detail';
import List from './List';

export default StackNavigator({
    Home: { //key为这个组件的标识
        screen: Home
    },
    Detail: {
        screen: Detail //引入的组件写在这里
    },
    List: {
        screen: List,
        navigationOptions: { //每个页面的一些配置,可以写在这里,也可以写在具体的组件里面
            title: '列表标题' 
        }
    }
});

每个组件的配置写在具体的组件里面:

export default class List extends Component{
    static navigationOptions = {
        title: 'List',
        //header: null  这个参数可以隐藏头部

    };
}

跳转到某个页面:

render() {
    const { navigate, state } = this.props.navigation;
    return (
        <View style={styles.container}>
            <Text style={styles.welcome}>
                Welcome to React Home!
            </Text>
            <Button
                onPress= {() => {
                    navigate('Detail', {user_name: "YINYI"}) //跳转到Detail组件 并传了一个user_name的参数给Detail组件
                }}
                title="跳转到Detail"
                color= "#f40"
            />
        </View>
    );
}

在Detai也里面使用接收的参数:

render() {
    const {navigator, state,} = this.props.navigation;
    return (
        <View style={styles.container}>
            <Button
                onPress= {() => {
                    navigator('List')
                }}
                title="跳转到List"
                color= "#f40"
            />
            <Text>{state.params.user_name}</Text> //这里使用Home传过来的参数
        </View>
    );
}

TabNavigator 流程

注册Tab各个组件

import { TabNavigator } from 'react-navigation';
import Detail from '../Detail/Detail';
import List from '../../../List';
export default Home = TabNavigator({
    Detail: {
        screen: Detail,
        navigationOptions: {
            title: '详情',
            tabBarLabel: '详情',
            tabBarIcon: () => {
                return(<Image
                    source={
                            require('../../static/minus.png')
                        }
                    style={styles.icon}// {tintColor: tintColor} 选中的图片和文字颜色
                />)
            }
        }
    },
    List: {
        screen: List,
        navigationOptions: {
            title: '我的',
            tabBarLabel: '我的',
            tabBarIcon: ({ tintColor }) => (
                <Image
                    source={
                            require('../../static/add.png')
                        }
                    style={styles.icon}// {tintColor: tintColor} 选中的图片和文字颜色
                />
            )
        }
    },
}, {
    tabBarPosition: 'bottom', // 显示在底端,android 默认是显示在页面顶端的
    swipeEnabled: true, // 是否左右滑动,如果有DrawerNavigator,最好设置为false避免手势冲突
    tabBarOptions: { //这里设置一些tabBar的属性
        activeTintColor: '#f40', // 文字和图片选中颜色
        inactiveTintColor: '#999', // 文字和图片默认颜色
        showIcon: true, // android 默认不显示 icon, 需要设置为 true 才会显示
        indicatorStyle: {height: 0}, // android TabBar下面会显示一条线,高度设为 0 后就不显示线了, 不知道还有没有其它方法隐藏???
        style: {
            backgroundColor: '#fff', // TabBar 背景色
            height:50
        },
        labelStyle: {
            fontSize: 12, // 文字大小,
            marginTop: 0,
        },
    },
});

将这个容器添加到router

import { StackNavigator } from 'react-navigation';
import Home from './src/container/Home/Home';
import Content from './src/container/Content/Content';

export default StackNavigator({
    Home: {
        screen: Home,
        navigationOptions: {
            title: "HomePage"
        }
    },
    Content: {
        screen: Content,
        navigationOptions: {
            headerTitle: 'ContentPage'
        }
    }
});

TabNavigator里面的每个组件可以跳转StackNavigator里的页面,这样可以嵌套很多可能,但是要跳转的页面必须在StackNavigator里面注册,比如这里在Detail里面跳转到Content:

export default class Detail extends Component{
    render() {
        return (
            <View style={styles.container}>
                <Text>Detail Page</Text>
                <Button
                    title= "跳转到Content"
                    color= "#f40"
                    onPress= {() => {
                        this.props.navigation.navigate('Content', {content: 'Content的详细内容...'})
                    }}
                />
            </View>
        );
    }
}

DrawerNavigator流程:

注册一个DrawerNavigator,里面包含了两个界面:

const CustomDrawerContentComponent = (props) => (
    <ScrollView>
        {/*<DrawerItems {...props} />*/}
        <View>
            <Text>46645646546</Text>
        </View>
    </ScrollView>
);
const MyDraw = DrawerNavigator({
        Home: {
            screen: MyHomeScreen,
        },
        Notifications: {
            screen: Order,
        },
    },
    {
        //drawerWidth: 200, // 抽屉宽
        drawerPosition: 'left', // 抽屉在左边还是右边
        contentComponent: CustomDrawerContentComponent,  // 自定义抽屉组件
        // contentOptions: {
        //     initialRouteName: MyHomeScreen, // 默认页面组件
        //     activeTintColor: 'white',  // 选中文字颜色
        //     activeBackgroundColor: '#ff8500', // 选中背景颜色
        //     inactiveTintColor: '#666',  // 未选中文字颜色
        //     inactiveBackgroundColor: '#fff', // 未选中背景颜色
        //     style: {  // 样式
        //
        //     }
        // }
    });

module.exports = MyDraw;

*在Drawer里注册的页面只能跳转到Drawer里面注册的其他页面。

在另一个页面里展现DrawerNavigator:

export default class Content extends Component{

    render() {
        return (
            <Drawer />
        );
    }
}

将这个页面放在之前的StackNavigator里就可以通过访问这个页面(这里的content)去访问Drawer里包含的页面了:

export default StackNavigator({
    Home: {
        screen: Home,
        navigationOptions: {
            title: "HomePage"
        }
    },
    Content: {
        screen: Content,
        navigationOptions: {
            header: null
        }
    },
    Detail: {
        screen: Detail
    }
});

路由跳转并清空路由记录:

import { NavigationActions } from 'react-navigation'
 resetAction = NavigationActions.reset({
                index: 0,
                actions: [
                    NavigationActions.navigate({routeName:'xxx'})//要跳转到的页面名字
                ]
            });
this.props.navigation.dispatch(resetAction);

最后附上一些参数说明(这一部分属于转载内容):

1、StackNavigator属性介绍

[html]  view plain  copy
  1. navigationOptions:配置StackNavigator的一些属性。  
  2.   
  3.     title:标题,如果设置了这个导航栏和标签栏的title就会变成一样的,不推荐使用  
  4.     header:可以设置一些导航的属性,如果隐藏顶部导航栏只要将这个属性设置为null  
  5.     headerTitle:设置导航栏标题,推荐  
  6.     headerBackTitle:设置跳转页面左侧返回箭头后面的文字,默认是上一个页面的标题。可以自定义,也可以设置为null  
  7.     headerTruncatedBackTitle:设置当上个页面标题不符合返回箭头后的文字时,默认改成"返回"  
  8.     headerRight:设置导航条右侧。可以是按钮或者其他视图控件  
  9.     headerLeft:设置导航条左侧。可以是按钮或者其他视图控件  
  10.     headerStyle:设置导航条的样式。背景色,宽高等  
  11.     headerTitleStyle:设置导航栏文字样式  
  12.     headerBackTitleStyle:设置导航栏‘返回’文字样式  
  13.     headerTintColor:设置导航栏颜色  
  14.     headerPressColorAndroid:安卓独有的设置颜色纹理,需要安卓版本大于5.0  
  15.     gesturesEnabled:是否支持滑动返回手势,iOS默认支持,安卓默认关闭  
  16.    
  17.   
  18. screen:对应界面名称,需要填入import之后的页面  
  19.   
  20. mode:定义跳转风格  
  21.   
  22.    card:使用iOS和安卓默认的风格  
  23.   
  24.    modal:iOS独有的使屏幕从底部画出。类似iOS的present效果  
  25.   
  26. headerMode:返回上级页面时动画效果  
  27.   
  28.    float:iOS默认的效果  
  29.   
  30.    screen:滑动过程中,整个页面都会返回  
  31.   
  32.    none:无动画  
  33.   
  34. cardStyle:自定义设置跳转效果  
  35.   
  36.    transitionConfig: 自定义设置滑动返回的配置  
  37.   
  38.    onTransitionStart:当转换动画即将开始时被调用的功能  
  39.   
  40.    onTransitionEnd:当转换动画完成,将被调用的功能  
  41.   
  42. path:路由中设置的路径的覆盖映射配置  
  43.   
  44. initialRouteName:设置默认的页面组件,必须是上面已注册的页面组件  
  45.   
  46. initialRouteParams:初始路由参数  


:大家可能对于path不太理解。path属性适用于其他app或浏览器使用url打开本app并进入指定页面。path属性用于声明一个界面路径,例如:【/pages/Home】。此时我们可以在手机浏览器中输入:app名称://pages/Home来启动该App,并进入Home界面。


2、TabNavigator属性介绍

[html]  view plain  copy
  1. screen:和导航的功能是一样的,对应界面名称,可以在其他页面通过这个screen传值和跳转。  
  2.   
  3.   
  4. navigationOptions:配置TabNavigator的一些属性  
  5.   
  6. title:标题,会同时设置导航条和标签栏的title  
  7.   
  8. tabBarVisible:是否隐藏标签栏。默认不隐藏(true)  
  9.   
  10. tabBarIcon:设置标签栏的图标。需要给每个都设置  
  11.   
  12. tabBarLabel:设置标签栏的title。推荐  
  13.   
  14. 导航栏配置  
  15.   
  16. tabBarPosition:设置tabbar的位置,iOS默认在底部,安卓默认在顶部。(属性值:'top','bottom')  
  17.   
  18. swipeEnabled:是否允许在标签之间进行滑动  
  19.   
  20. animationEnabled:是否在更改标签时显示动画  
  21.   
  22. lazy:是否根据需要懒惰呈现标签,而不是提前,意思是在app打开的时候将底部标签栏全部加载,默认false,推荐为true  
  23.   
  24. trueinitialRouteName: 设置默认的页面组件  
  25.   
  26. backBehavior:按 back 键是否跳转到第一个Tab(首页), none 为不跳转  
  27.   
  28. tabBarOptions:配置标签栏的一些属性iOS属性  
  29.   
  30. activeTintColor:label和icon的前景色 活跃状态下  
  31.   
  32. activeBackgroundColor:label和icon的背景色 活跃状态下  
  33.   
  34. inactiveTintColor:label和icon的前景色 不活跃状态下  
  35.   
  36. inactiveBackgroundColor:label和icon的背景色 不活跃状态下  
  37.   
  38. showLabel:是否显示label,默认开启 style:tabbar的样式  
  39.   
  40. labelStyle:label的样式安卓属性  
  41.   
  42. activeTintColor:label和icon的前景色 活跃状态下  
  43.   
  44. inactiveTintColor:label和icon的前景色 不活跃状态下  
  45.   
  46. showIcon:是否显示图标,默认关闭  
  47.   
  48. showLabel:是否显示label,默认开启 style:tabbar的样式  
  49.   
  50. labelStyle:label的样式 upperCaseLabel:是否使标签大写,默认为true  
  51.   
  52. pressColor:material涟漪效果的颜色(安卓版本需要大于5.0)  
  53.   
  54. pressOpacity:按压标签的透明度变化(安卓版本需要小于5.0)  
  55.   
  56. scrollEnabled:是否启用可滚动选项卡 tabStyle:tab的样式  
  57.   
  58. indicatorStyle:标签指示器的样式对象(选项卡底部的行)。安卓底部会多出一条线,可以将height设置为0来暂时解决这个问题  
  59.   
  60. labelStyle:label的样式  
  61.   
  62. iconStyle:图标样式  

3、DrawerNavigator属性介绍
  

[html]  view plain  copy
  1. DrawerNavigatorConfig  
  2.   
  3.     drawerWidth - 抽屉的宽度  
  4.     drawerPosition - 选项是左或右。 默认为左侧位置  
  5.     contentComponent - 用于呈现抽屉内容的组件,例如导航项。 接收抽屉的导航。 默认为DrawerItems  
  6.     contentOptions - 配置抽屉内容  
  7.   
  8.     initialRouteName - 初始路由的routeName  
  9.     order - 定义抽屉项目顺序的routeNames数组。  
  10.     路径 - 提供routeName到路径配置的映射,它覆盖routeConfigs中设置的路径。  
  11.     backBehavior - 后退按钮是否会切换到初始路由? 如果是,设置为initialRoute,否则为none。 默认为initialRoute行为  
  12.   
  13.    DrawerItems的contentOptions属性  
  14.   
  15.     activeTintColor - 活动标签的标签和图标颜色  
  16.     activeBackgroundColor - 活动标签的背景颜色  
  17.     inactiveTintColor - 非活动标签的标签和图标颜色  
  18.     inactiveBackgroundColor - 非活动标签的背景颜色  
  19.     内容部分的样式样式对象  
  20.     labelStyle - 当您的标签是字符串时,要覆盖内容部分中的文本样式的样式对象  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值