React Native开发React Native高手进阶React Native学习

React Navigation 3x系列教程』createMa

2019-01-07  本文已影响2人  CrazyCodeBoy

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

createMaterialTopTabNavigator.png

createMaterialTopTabNavigator API

createMaterialTopTabNavigator(RouteConfigs, TabNavigatorConfig):

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

RouteConfigs

RouteConfigs支持三个参数screenpath以及navigationOptions

TabNavigatorConfig

tabBarOptions(tab配置)

eg:

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

navigationOptions(屏幕导航选项)

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

【案例1】使用createMaterialTopTabNavigator做界面导航、配置navigationOptions

createBottomTabNavigator

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

export const MaterialTopTabNavigator = createMaterialTopTabNavigator({//在这里配置页面的路由
        Page1: {
            screen: Page1,
            navigationOptions: {
                tabBarLabel: 'Page10',
                tabBarIcon: ({tintColor, focused}) => (
                    <Ionicons
                        name={'ios-home'}
                        size={26}
                        style={{color: tintColor}}
                    />
                ),
            }
        },
        Page4: {
            screen: Page4,
            navigationOptions: {
                tabBarLabel: 'Page2',
                tabBarIcon: ({tintColor, focused}) => (
                    <Ionicons
                        name={'ios-people'}
                        size={26}
                        style={{color: tintColor}}
                    />
                ),
            }
        },
        Page3: {
            screen: Page3,
            navigationOptions: {
                tabBarLabel: 'Page3',
                tabBarIcon: ({tintColor, focused}) => (
                    <Ionicons
                        name={'ios-chatboxes'}
                        size={26}
                        style={{color: tintColor}}
                    />
                ),
            }
        },
    },
    {
        tabBarOptions: {
            tabStyle: {
                minWidth: 50
            },
            upperCaseLabel: false,//是否使标签大写,默认为true
            scrollEnabled: true,//是否支持 选项卡滚动,默认false
            // activeTintColor: 'white',//label和icon的前景色 活跃状态下(选中)
            // inactiveTintColor: 'gray',//label和icon的前景色 活跃状态下(未选中)
            style: {
                backgroundColor: '#678',//TabBar 的背景颜色
            },
            indicatorStyle: {
                height: 2,
                backgroundColor: 'white',
            },//标签指示器的样式
            labelStyle: {
                fontSize: 13,
                marginTop: 6,
                marginBottom: 6,
            },//文字的样式
        },
    }
)

第二步:配置navigationOptions:

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

Page2: {
    screen: Page2,
    navigationOptions: {
        tabBarLabel: 'Page2',
        tabBarIcon: ({tintColor, focused}) => (
            <Ionicons
                name={focused ? 'ios-people' : 'ios-people-outline'}
                size={26}
                style={{color: tintColor}}
            />
        ),
    }
},

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

第三步:界面跳转

 const {navigation} = this.props;
 ...
 <Button
    title="跳转到页面4"
    onPress={() => {
        navigation.navigate("Page4",{ name: 'Devio' })
    }}
/>

代码解析:

页面跳转可分为两步:

【高级案例】react-navigation的高级应用

在使用react-navigation时往往有些需求通过简单的配置是无法完成的,比如:

类似上述的应用场景有很多,大家可以通过与本教程配套的最新版React Native+Redux打造高质量上线App视频教程进行进一步学习react-navigation的更多高级应用

上一篇 下一篇

猜你喜欢

热点阅读