React Native高手进阶React Native学习

『React Navigation 3x系列教程』createB

2018-12-30  本文已影响36人  CrazyCodeBoy
createBottomTabNavigator开发指南

期待已久的新教程上线啦!解锁React Native开发新姿势,一网打尽React Native最新与最热技术,点我Get!!!

createBottomTabNavigator相当于iOS里面的TabBarController,屏幕下方的标签栏。如图:

createBottomTabNavigator.png

createBottomTabNavigator API

createBottomTabNavigator(RouteConfigs, BottomTabNavigatorConfig):

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

RouteConfigs

RouteConfigs支持三个参数screenpath以及navigationOptions

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

BottomTabNavigatorConfig

tabBarOptions(tab配置)

eg:

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

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

navigationOptions(屏幕导航选项)

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

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

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

createBottomTabNavigator

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

export const AppTabNavigator = createBottomTabNavigator({
    Page1: {
        screen: Page1,
        navigationOptions: {
            tabBarLabel: 'Page1',
            tabBarIcon: ({tintColor, focused}) => (
                <Ionicons
                    name={focused ? 'ios-home' : 'ios-home-outline'}
                    size={26}
                    style={{color: tintColor}}
                />
            ),
        }
    },
    Page2: {
        screen: Page2,
        navigationOptions: {
            tabBarLabel: 'Page2',
            tabBarIcon: ({tintColor, focused}) => (
                <Ionicons
                    name={focused ? 'ios-people' : 'ios-people-outline'}
                    size={26}
                    style={{color: tintColor}}
                />
            ),
        }
    },
    Page3: {
        screen: Page3,
        navigationOptions: {
            tabBarLabel: 'Page3',
            tabBarIcon: ({tintColor, focused}) => (
                <Ionicons
                    name={focused ? 'ios-chatboxes' : 'ios-chatboxes-outline'}
                    size={26}
                    style={{color: tintColor}}
                />
            ),
        }
    },
}, {
    tabBarComponent: TabBarComponent,
    tabBarOptions: {
        activeTintColor: Platform.OS === 'ios' ? '#e91e63' : '#fff',
    }
});

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

第二步:配置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 组件,大家可以根据需要进行定制:

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

第三步:界面跳转

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

代码解析:

页面跳转可分为两步:

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

第四步:更新页面Params与返回

export default class Page1 extends React.Component {
    //也可在这里定义每个页面的导航属性,这里的定义会覆盖掉别处的定义
    // static navigationOptions = {
    //     title: 'Page1',
    // };

    render() {
        const {navigation} = this.props;
        return <View style={{flex: 1, backgroundColor: "gray",}}>
            <Text style={styles.text}>欢迎来到Page1</Text>
            <Button
                title="Go Back"
                onPress={() => {
                    navigation.goBack();
                }}
            />
            <Button
                title="改变主题色"
                onPress={() => {
                    navigation.setParams({theme:{
                        tintColor:'orange',
                        updateTime:new Date().getTime()
                    }})
                }}
            />
            <Button
                title="跳转到页面2"
                onPress={() => {
                    navigation.navigate("Page2")
                }}
            />
        </View>
    }
}

代码解析:

在上述代码中通过:

<Button
    title="改变主题色"
    onPress={() => {
        navigation.setParams({theme:{
            tintColor:'orange',
            updateTime:new Date().getTime()
        }})
    }}
/>

更新当前主题,你会看到当点击“改变主题色“按钮时,TabBar的颜色也会跟着改变。

当用户单击Go Back按钮时,通过:

navigation.goBack();

实现了返回到默认的Tab。

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

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

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

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

上一篇 下一篇

猜你喜欢

热点阅读