• react-native navigation的学习与使用


    在很久之前,RN中文网说推荐用react-navigation替代navigator作为新的导航库,从RN 0.43版本开始,官方就已经停止维护Navigator了,所以强烈建议大家迁移到新的react-navigation库,而且新的导航库无论从性能还是易用性上都要大大好于老的Navigator!

    附上文档地址 

    接下来我们看react-navigation的使用。

    一 StackNavigator

    1.下载和安装

     在项目根目录执行:

      npm install --save react-navigation

     

    2.在js文件中引入我们需要的组件

       import {StackNavigator} from 'react-navigation';
     

    3.开始使用

     
    import Home from './Home';
    import Mains from './Second';
    [html] view plain copy
     
    1. <span style="font-size:24px;">const App = StackNavigator({  
    2.     Home: {screen: Home},  
    3.     Chat: {screen: Second},  
    4.     Two: {screen: Mains}  
    5. });  
    6.   
    7. export default App;</span>  
    在index.android.js 或者index.ios.js中引入app
     
    在StackNavigator中 把我们需要跳转的页面都加进去。
    然后给每个页面设置标题
    export default class Home extends React.Component {


        static navigationOptions = {
            title: 'Welcome',
        };

    会在标题栏显示 Welcome的字样。
    接下来是跳转
    使用navigate()方法来执行跳转
     
     navigate('Chat', {user:user})
    前面chat为你定义的页面名字,后面{}内包括你要传的参数,可为空,
    例如  navigate('Chat'),
    代码如下:

    [javascript] view plain copy
     
    1. <span style="font-size:24px;">    render() {  
    2.         const {navigate} = this.props.navigation;  
    3.         let user = {'name': 'zy', age: 18}  
    4.         return (  
    5.             <View>  
    6.                 <Text onPress={() => navigate('Chat', {user:user})}>Home</Text>  
    7.             </View>  
    8.         )  
    9.     }  
    10. }</span>  

    在Chat页面写接收参数的方法:
     
    render() {
            const {navigate} = this.props.navigation;
            let {params} = this.props.navigation.state;
            return (
                <View>
                    <Text>Second {params.user.name}</Text>
                </View>
            )
        }
     
    这样,text组件上就会显示你传过来的参数,例如我之前传来的是name为zy的一个对象,那么这里就会显示出Second zy。
     
    从第一个页面跳转到第二个页面的话,第二个页面会有个回退按钮,返回到前一个页面。
     
     
     
    这个navigationOptions也可以在最开始的StackNavigator中配置:
     
    const App = StackNavigator({
        Home: {
            screen: Home,
            navigationOptions: {
                headerTitle: 'Home',   //headerTitletitle效果相同 设置导航栏标题,推荐用这个方法
    headerTitleStyle: {color: 'red', backgroundColor: 'white', alignSelf: 'center'},   //标题栏文字样式 设置居中
    headerStyle: {backgroundColor: 'green'},  //导航栏的样式
    //header: null                              //隐藏导航栏
    headerRight: <Text onPress={() => {
                    alert('i am right');
                }
                }>right</Text>,                          //标题栏左右的按钮
    headerLeft: <Text style={{marginLeft: 10}}>left</Text>,           //最好定义在组件内部
    }
        },
        First: {
            screen: First,
            navigationOptions: ({navigation}) => ({
                headerTitle: 'i am first',
                headerStyle: {backgroundColor: 'green'},  //导航栏的样式
    headerTitleStyle: {color: 'red', alignSelf: 'center', backgroundColor: 'white'},
                headerLeft: <Button title={'go back'} onPress={() => {
                    navigation.goBack();
                }}/>,
            })
    
        },
        Second: {
            screen: Second,
            navigationOptions: {}
        },
        Tab: {
            screen: Tab,
            navigationOptions: {
                title: 'Tab',
            }
        },
    });
    上面在通用属性中,配置了一个右侧按钮,那么在每一个页面都会有这样一个按钮
    它的属性有:

    StackNavigatorConfig  

    路由器的选项:

    • initialRouteName - 设置堆栈的默认屏幕。必须匹配路由配置中的一个密钥。也就是已有的路由名字
    • initialRouteParams - 初始路线的参数
    • navigationOptions - 用于屏幕的默认导航选项
    • paths - 路由配置中设置的路径的覆盖映射

    视觉选项:

    • mode - 定义渲染和转换的样式:
      • card - 使用标准的iOS和Android屏幕转换。这是默认值。
      • modal - 使屏幕从底部滑入,这是普通的iOS模式。只适用于iOS,对Android无影响。
    • headerMode - 指定标题应如何呈现:
      • float - 渲染一个保持在顶部的标题,并且随着画面的改变而呈现动画。这是iOS上的常见模式。
      • screen - 每个屏幕都有一个标题,标题与屏幕一起淡入淡出。这是Android上的常见模式。
      • none - 不会显示标题。
    • cardStyle - 使用此支路覆盖或扩展堆叠中单个卡的默认样式。
    • transitionConfig - 返回覆盖默认屏幕转换的对象的功能。
    • onTransitionStart - 卡转换动画即将开始时调用的功能。
    • onTransitionEnd - 卡转换动画完成后要调用的功能。
    接下来看一下navigationOptions的参数:

    title 

    串可以用作回退和headerTitletabBarLabel

    React元素或给定的函数返回一个React元素,以显示为标题。设置隐藏标题。HeaderPropsnull

    headerTitle 

    标题使用的字符串或React元素。默认为场景title

    headerBackTitle 

    iOS上的后退按钮使用的标题字符串或禁用标签。默认为场景nulltitle

    headerTruncatedBackTitle 

    当返回按钮不适合屏幕时使用的标题字符串。默认。headerBackTitle"Back"

    headerRight 

    反应元素显示在标题的右侧

    headerLeft 

    反应元素显示在标题的左侧

    headerStyle 

    标题的样式对象

    headerTitleStyle 

    标题组件的样式对象

    headerBackTitleStyle 

    样式对象为后面的标题

    headerTintColor 

    标题颜色

    headerPressColorAndroid 

    颜色纹理(Android> = 5.0)

    gesturesEnabled 

    是否可以使用手势关闭此屏幕。在iOS上默认为true,在Android上为false。

    二. TabNavigator

    然后我们看一下另一个常用组件TabNavigator的使用,用于带有多个选项卡的屏幕,相当于android中的tab,可以来回切换。

    1.引入组件

     
    import {TabNavigator} from 'react-navigation';
     

    2.开始使用

    可以给每个页面单独设置标题,切换到对应的tab会自动切换标题,也可以用从前一个页面传来的参数中的内容当标题
     
     
    class Second extends React.Component {


        static navigationOptions = ({navigation}) => ({
            title: ` ${navigation.state.params.user.name}`,       //前一个页面传来的对象的name属性
        })


        render() {
            const {navigate} = this.props.navigation;
            let {params} = this.props.navigation.state;
            return (
                <View>
                    <Text onPress={() => navigate('Home')}>Second {params.user.name}</Text>
                </View>
            )
        }
    }


    class Three extends React.Component {
        static navigationOptions = ({navigation}) => ({
            title: ` ${navigation.state.params.user.age}`,
        })

        render() {
            const {navigate} = this.props.navigation;
            let {params} = this.props.navigation.state;
            var name = params.name;
            return (
                <View>
                    <Text>three : {params.user.age}</Text>
                </View>
            )
        }
    }
     
    const MainSceen = TabNavigator({
        l1: {screen: Second},
        l2: {screen: Three}
    })
    export default MainSceen;
     
    当然,这样做之后我们要把 MainSceen 这个组件添加到StackNavigator中,
    像这样:
    [html] view plain copy
     
    1. <span style="font-size:24px;">const App = StackNavigator({  
    2.     Home: {screen: Home},  
    3.   //  Chat: {screen: Second},  
    4.     Two: {screen: Mains}  
    5. });</span>  

    再跳转的话直接navigate('Two')就可以,如果你不需要传递参数的话。
     
    然后我们给标题栏添加个右边的按钮
    static navigationOptions = ({navigation}) => ({
        title: ` ${navigation.state.params.user.name}`,
        headerRight:<Button title={'标题'}/>
    })
    当然你也可以换成其他组件,也可以添加点击事件等等。也可以一起配。
    const MyApp = TabNavigator({
            Home: {
                screen: Home,
                navigationOptions: {  //也可以写在组件内部的static navigationOptionstabBarLabel: '首页',
                    tabBarIcon: ({tintColor}) => (<Image source={require('./images/camera.png')}
                                                         style={{tintColor: tintColor}}/>)
                }
    
            },
            Notifications: {
                screen: Two,
                navigationOptions: {
                    tabBarLabel: '2',
                    tabBarIcon: ({tintColor}) => (<Image source={require('./images/add.png')}
                                                         style={[{tintColor: tintColor}, styles.icon]}/>)
                }
    
            },
            Me: {
                screen: Three,
                navigationOptions: {
                    tabBarLabel: '3',
                    tabBarIcon: ({tintColor}) => (<Image source={require('./images/more.png')}
                                                         style={{tintColor: tintColor}}/>)
                }
            }
        },
        {
            animationEnabled: false, // 切换页面时是否有动画效果
    tabBarPosition: 'bottom', // 显示在底端,android 默认是显示在页面顶端的
    swipeEnabled: true, // 是否可以左右滑动切换tab
    backBehavior: 'none', //  back 键是否跳转到第一个Tab(首页) none 为不跳转
    tabBarOptions: {
                activeTintColor: '#ff8500', // 文字和图片选中颜色
    inactiveTintColor: '#999', // 文字和图片未选中颜色
    showIcon: true, // android 默认不显示 icon, 需要设置为 true 才会显示
    indicatorStyle: {
                    height: 0  // TabBar下面显示有一条线,可以设高度为0后隐藏
    },
                style: {
                    backgroundColor: '#fff', // TabBar 背景色
    // height: 44
    },
                labelStyle: {
                    fontSize: 10, // 文字大小
    },
            },
        });

    3.常用属性介绍

    再看一下API

    API定义 

    TabNavigator(RouteConfigs, TabNavigatorConfig)

    第一个参数RouteConfigs  :

    RouteConfigs  对象是从路由名称映射到一个路由配置,它告诉导航以呈现该路线什么,参照 StackNavigator
     

    第二个参数:TabNavigatorConfig  

    • tabBarComponent- 要用作标签栏的组件,例如 (这是iOS上的默认设置), (这是Android上的默认设置)TabBarBottomTabBarTop
    • tabBarPosition- 标签栏的位置可以是或'top''bottom'
    • swipeEnabled - 是否允许在标签之间进行滑动
    • animationEnabled - 是否在更改标签时进行动画处理
    • lazy - 是否根据需要懒惰呈现标签,而不是提前制作
    • tabBarOptions - 配置标签栏,如下所示。

    几个选项被传递到底层路由器来修改导航逻辑:

    • initialRouteName - 第一次加载时初始标签路由的routeName
    • order - 定义选项卡顺序的routeNames数组
    • paths - 将routeName映射到路径配置,该配置将覆盖routeConfigs中设置的路径。
    • backBehavior - 后退按钮是否会使Tab键切换到初始选项卡?如果是的话,设置为,否则。默认为行为。initialRoutenoneinitialRoute

    tabBarOptionsfor (iOS上的默认标签栏)TabBarBottom 

    • activeTintColor - 活动标签的标签和图标颜色
    • activeBackgroundColor - 活动选项卡的背景颜色
    • inactiveTintColor - 不活动标签的标签和图标颜色
    • inactiveBackgroundColor - 非活动标签的背景颜色
    • showLabel - 是否显示标签的标签,默认为true
    • style - 标签栏的样式对象
    • labelStyle - 选项卡标签的样式对象
    • tabStyle - 标签的样式对象

    例:

    tabBarOptions: {
      activeTintColor: '#e91e63',
      labelStyle: {
        fontSize: 12,
      },
      style: {
        backgroundColor: 'blue',
      },
    }
    

    tabBarOptionsfor (Android上的默认标签栏)TabBarTop 

    • activeTintColor - 活动标签的标签和图标颜色
    • inactiveTintColor - 不活动标签的标签和图标颜色
    • showIcon - 是否显示标签的图标,默认值为false
    • showLabel - 是否显示标签的标签,默认为true
    • upperCaseLabel - 是否使标签大写,默认为true
    • pressColor - 颜色纹理(Android> = 5.0)
    • pressOpacity - 按压标签的不透明度(iOS和Android <5.0 only)
    • scrollEnabled - 是否启用可滚动选项卡
    • tabStyle - 标签的样式对象
    • indicatorStyle - 标签指示器的样式对象(选项卡底部的行)
    • labelStyle - 选项卡标签的样式对象
    • iconStyle - 标签图标的样式对象
    • style - 标签栏的样式对象

    例:

    tabBarOptions: {
      labelStyle: {
        fontSize: 12,
      },
      tabStyle: {
        width: 100,    
      },
      style: {
        backgroundColor: 'blue',
      },
    }
    

    NavigationOptions:
     

    title 

    通用标题可以用作备用和headerTitletabBarLabel

    tabBarVisible 

    True或false显示或隐藏选项卡栏,如果未设置,则默认为true

    tabBarIcon 

    React Element或者一个给定的函数返回一个React.Element,以在tab栏中显示{ focused: boolean, tintColor: string }

    tabBarLabel 

    标签栏或React元素中显示的标签的标题字符串或者给定的函数返回一个React.Element,以在标签栏中显示。当未定义时,使用场景。要隐藏,请参见上一节。{ focused: boolean, tintColor: string }titletabBarOptions.showLabel

    三.DrawerNavigator 
     

    用于轻松设置带抽屉导航的屏幕。有关实例,请参阅我们的展会演示

    class MyHomeScreen extends React.Component {
      static navigationOptions = {
        drawerLabel: 'Home',
        drawerIcon: ({ tintColor }) => (
          <Image
            source={require('./chats-icon.png')}
            style={[styles.icon, {tintColor: tintColor}]}
          />
        ),
      };
    
      render() {
        return (
          <Button
            onPress={() => this.props.navigation.navigate('Notifications')}
            title="Go to notifications"
          />
        );
      }
    }
    
    class MyNotificationsScreen extends React.Component {
      static navigationOptions = {
        drawerLabel: 'Notifications',
        drawerIcon: ({ tintColor }) => (
          <Image
            source={require('./notif-icon.png')}
            style={[styles.icon, {tintColor: tintColor}]}
          />
        ),
      };
    
      render() {
        return (
          <Button
            onPress={() => this.props.navigation.goBack()}
            title="Go back home"
          />
        );
      }
    }
    
    const styles = StyleSheet.create({
      icon: {
        width: 24,
        height: 24,
      },
    });
    
    const MyApp = DrawerNavigator({
      Home: {
        screen: MyHomeScreen,
      },
      Notifications: {
        screen: MyNotificationsScreen,
      },
    });
    

    打开和关闭抽屉,导航到并分别。'DrawerOpen''DrawerClose'

    this.props.navigation.navigate('DrawerOpen'); // open drawer
    this.props.navigation.navigate('DrawerClose'); // close drawer
    

    API定义 

    DrawerNavigator(RouteConfigs, DrawerNavigatorConfig)
    

    RouteConfigs  

    路由CONFIGS对象是从路由名称映射到一个路由配置,它告诉导航以呈现该路线什么,参见例如由。StackNavigator

    DrawerNavigatorConfig  

    • drawerWidth - 抽屉的宽度
    • drawerPosition- 选项是或。默认是位置。leftrightleft
    • contentComponent - 用于呈现抽屉内容的组件,例如导航项。接收抽屉的支柱。默认为。有关详细信息,请参阅下文。navigationDrawerItems
    • contentOptions - 配置抽屉内容,见下文。

    示例: 

    默认是不可滚动。要实现可滚动,您必须使用该属性定制容器,如下面的示例所示。DrawerViewViewcontentComponent

    {
      drawerWidth: 200,
      drawerPosition: 'right',
      contentComponent: props => <ScrollView><DrawerItems {...props} /></ScrollView>
    }
    

    几个选项被传递到底层路由器来修改导航逻辑:

    • initialRouteName - 初始路由的routeName。
    • order - 定义抽屉物品顺序的routeNames数组。
    • paths - 将routeName映射到路径配置,该配置将覆盖routeConfigs中设置的路径。
    • backBehavior - 后退按钮是否会切换到初始路线?如果是的话,设置为,否则。默认为行为。initialRoutenoneinitialRoute

    提供自定义contentComponent 

    您可以轻松地覆盖所使用的默认组件:react-navigation

    import { DrawerItems } from 'react-navigation';
    
    const CustomDrawerContentComponent = (props) => (
      <View style={style.container}>
        <DrawerItems {...props} />
      </View>
    );
    
    const styles = StyleSheet.create({
      container: {
        flex: 1,
      },
    });
    

    contentOptionsDrawerItems 

    • items - 路由数组,可以修改或覆盖
    • activeItemKey - 标识活动路线的钥匙
    • activeTintColor - 活动标签的标签和图标颜色
    • activeBackgroundColor - 活动标签的背景颜色
    • inactiveTintColor - 无效标签的标签和图标颜色
    • inactiveBackgroundColor - 非活动标签的背景颜色
    • onItemPress(route) - 按下项目时调用的功能
    • style - 内容部分的样式对象
    • labelStyle- 当您的标签是字符串时,样式对象将覆盖内容部分中的样式Text

    示例: 

    contentOptions: {
      activeTintColor: '#e91e63',
      style: {
        marginVertical: 0,
      }
    }
    

    屏幕导航选项 

    title 

    通用标题可以用作备用和headerTitledrawerLabel

    drawerLabel 

    String,React元素或给定的函数返回一个React.Element,显示在抽屉侧边栏中。当不确定,现场使用{ focused: boolean, tintColor: string }title

    drawerIcon 

    React Element或一个函数,返回一个React.Element,显示在抽屉侧边栏中{ focused: boolean, tintColor: string }

    创建的导航器组件需要以下道具:DrawerNavigator(...)

    • screenProps - 向儿童屏幕传递额外的选项,例如:
    const DrawerNav = DrawerNavigator({
      // config
    });
    
    <DrawerNav
      screenProps={/* this prop will get passed to the screen components and nav options as props.screenProps */}
    />
    

    嵌套DrawerNavigation 

    请记住,如果您嵌套DrawerNavigation,抽屉将显示在父导航下方。

    引用原文:http://blog.csdn.net/u011272795/article/details/72855899

    写博客是为了记住自己容易忘记的东西,另外也是对自己工作的总结,文章可以转载,无需版权。希望尽自己的努力,做到更好,大家一起努力进步!

    如果有什么问题,欢迎大家一起探讨,代码如有问题,欢迎各位大神指正!

  • 相关阅读:
    S5pv210 android VGA 1440*900 视频播放/3D 演示效果实拍视频
    毕业3年,工资从5k到20k的经历——真的还是假的啊?
    转载.WinCE6.0 Camera驱动整体结构
    半夜来认识一下S5PV210 的VBPDE和VFPDE
    微软的windows 8授权真的要这么贵?谁要?!
    基于S5PC100的FIMC的部分解释——一篇让我理解透彻2440和S5PV210 摄像头camera控制器的文章
    转.Buffer Management by the Camera Driver (Windows Embedded CE 6.0)
    微软?想干掉苹果,自己做平板,还想做手机,有优秀的合作伙伴诺基亚?简直是笑话,难怪鲍ceo 被评为最应该下台的CEO了
    魅族经过M8——》M9——》MX 已经走向国际?
    wince6.0 S5pv210 之Sate210 VGA 镜像1440*900@60HZ/1280*1024@70HZ分辨率测试镜像(南嵌电子科技作品)
  • 原文地址:https://www.cnblogs.com/summary-2017/p/7571813.html
Copyright © 2020-2023  润新知