RN---tabBar+Navigator项目结构

Gif.gif

注意事项

iOS中通常是UITabBarController + UINavigationController +Controller 实现选项卡与导航条,self.hidesBottomBarWhenPushed = YES实现隐藏二级界面tabBar。 RN中用iOS的思路则不能隐藏tabBar,是navigator -> tabBar -> component (tabBar当做普通组件)

导入第三方

打开终端,cd 到你的文件目录

// 导入tabBar
npm install react-native-tab-navigator --save
// 导入navigator
npm  install   react-native-deprecated-custom-components  --save
Navigator

创建NavigatorComponent 组件作为app入口,配置路由

import React,{Component} from 'react';
import {
    View,
    Text,
    StyleSheet
} from 'react-native';

/**
 *  navigator ---命令行:'npm  install   react-native-deprecated-custom-components  --save'
 */
import {Navigator} from "react-native-deprecated-custom-components";
import TabBar from './MainComponent'

export default class NavigatorComponent extends Component{
    render(){
        return(
            //有导航功能,没有类似iOS的导航条,二级界面隐藏 tabBar
            <Navigator
                style={styles.container}
                initialRoute = {{ name: 'TabBar', component: TabBar }}
                configureScene={()=>{
                    return Navigator.SceneConfigs.PushFromRight;
                }}
                renderScene={(route,navigator)=>{
                    let Component = route.component;
                    return <Component {...route.passProps} navigator={navigator}/>;
                }}
            />
        );
    }
}
const styles = StyleSheet.create({
    container:{
        flex:1
    }
});
TabBar

创建MainComponent组件作为TabBar,分别导入首页,我的等等组件

import React,{Component} from 'react';
import {
    View,
    Text,
    Image,
    StyleSheet,
    Platform,// 判断当前运行的系统
} from 'react-native';

/**
 *  导入第三方组件
 *  1,tabBar---- 命令行:'npm install react-native-tab-navigator --save'
 */
import TabNavigator from 'react-native-tab-navigator';

/**
 * 导入根组件
 */
import Home from '../Home/HomeComponent'
import Shop from '../Shop/ShopComponent'
import More from '../More/MoreComponent'
import Mine from '../Mine/MineComponent'



// iOS思路: tabBar ->  navigation -> contorller
// rn思路: navigator -> tabBar -> component (tabBar当做普通组件)
export default class MainComponent extends Component{
    // 状态机,记录选中的组件
    constructor(props){
        super(props);
        this.state={
            selectTab:'home'
        }
    }
    render(){
        return(
            <TabNavigator>
                {/*首页*/}
                {this._renderTabBarItem('首页','home','icon_tabbar_homepage','icon_tabbar_homepage_selected','首页',Home)}
                {/*商家*/}
                {this._renderTabBarItem('商家','shop','icon_tabbar_merchant_normal','icon_tabbar_merchant_selected','商家',Shop)}
                {/*我的*/}
                {this._renderTabBarItem('我的','mine','icon_tabbar_mine','icon_tabbar_mine_selected','我的',Mine)}
                {/*更多*/}
                {this._renderTabBarItem('更多','more','icon_tabbar_misc','icon_tabbar_misc_selected','更多',More)}
            </TabNavigator>
        );
    }

    // 抽出创建TabNavigator方法
    _renderTabBarItem(itemTitle,selectTabName,normalName,selectedName,componentName,component){
        return(
            <TabNavigator.Item
                title={itemTitle}
                selectedTitleStyle={styles.selectedTitleStyle}
                selected={this.state.selectTab=== selectTabName}
                renderIcon={()=><Image source={{uri:normalName}} style={styles.iconStyle}/>}
                renderSelectedIcon={()=><Image source={{url:selectedName}} style={styles.iconStyle}/>}
                onPress={()=>{
                    this.setState({
                        selectTab:selectTabName
                    })
                }}
            >
                {this._renderComponent(selectTabName)}
            </TabNavigator.Item>
        );
    }
    _renderComponent(index){
        if (index==='home'){
            return(<Home navigator={this.props.navigator}/>);
        } else if (index === 'shop'){
            return(<Shop navigator={this.props.navigator}/>);
        } else if (index === 'mine'){
            return(<Mine navigator={this.props.navigator}/>);
        } else {
            return(<More navigator={this.props.navigator}/>);
        }
    }
}
const styles = StyleSheet.create({
    iconStyle:{
        width: Platform.OS === 'ios'? 25:20,
        height:Platform.OS === 'ios'? 25:20,
    },
    selectedTitleStyle:{
        color:'orange'
    }
});

demo传送门 我的博客

转载于:https://my.oschina.net/iOScoderZhao/blog/1837424

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值