TabNavigator自定义Tab

本文探讨了在react-navigation中使用TabNavigation时遇到的样式问题,特别是Tab样式无法如预期居中的困扰。通过自定义Tab组件,解决了这个问题,确保在不同设备上能够正确适配并达到理想效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

react navigation三种导航,其中TabNavigation和StackNavigation用的比较多。今天要说的就是关于TabNavigation的样式问题,可能很多人都遇到过为什么明明设置了style和tabStyle还是会有问题。
我想可能会有很多人这样写:

const tabbaroption = {
    activeTintColor: Color.f3474b,
    inactiveTintColor: Color.C7b7b7b,
    showIcon: true,
    style: {
        backgroundColor:'white'
    },
    indicatorStyle: {
        opacity: 0
    },
    iconStyle:{
        paddingTop:0,
        padding:0,
        marginTop:0,
        width:SCALE(45),
        height:SCALE(45),
    },
    labelStyle:{
        marginTop:0,
        padding:0,
    },
    tabStyle: {
        height:Platform.OS==='ios'?SCALE(90):SCALE(100),
        alignItems:'center',
        justifyContent:'center',

    }
};
//我发起的请假选项
const LeaveListIndex = TabNavigator(
    {
        LeaveUnCheckList: { screen: LeaveUnCheckList },
        LeaveFinished:{screen: LeaveFinished}
    },
    {
        lazy: true,
        tabBarPosition: 'bottom',
        animationEnabled: true,
        tabBarOptions: tabbaroption,
        headerLeft:null,
    });

然后满心欢喜的以为图片和文字会居中,然而残酷的现实告诉我们并不是这样。得到的结果大概是这样的。
tabstyle.png

这个时候你可能会设置各种背景颜色,设置margin,设置padding距离,然后发现终于成功了,在其他机型运行时又tm出现适配问题。哈哈哈哈。
所以今天我们就自己定义一个tab替换掉默认的。
修改代码:

const LeaveListIndex = TabNavigator(
    {
        LeaveUnCheckList: { screen: LeaveUnCheckList },
        LeaveFinished:{screen: LeaveFinished}
    },
    {
        lazy: true,
        tabBarComponent:props => <Tab {...props}/>,
        tabBarPosition: 'bottom',
        animationEnabled: true,
        tabBarOptions: tabbaroption,
        headerLeft:null,
    });

自定义的Tab:

export default class Tab extends Component {
    renderItem = (route, index) => {
        const {
            navigation,
            jumpToIndex,
        } = this.props;

        const focused = index === navigation.state.index;
        const color = focused ? this.props.activeTintColor : this.props.inactiveTintColor;
        let TabScene = {
            focused:focused,
            route:route,
            tintColor:color
        };
        return (
            <TouchableOpacity
                key={route.key}
                style={styles.tabItem}
                onPress={() => jumpToIndex(index)}
            >
                <View
                    style={styles.tabItem}>
                    {this.props.renderIcon(TabScene)}
                    <Text style={{ ...styles.tabText,marginTop:SCALE(10),color }}>{this.props.getLabel(TabScene)}</Text>
                </View>
            </TouchableOpacity>
        );
    };
    render(){
        const {navigation,} = this.props;
        const {routes,} = navigation.state;
        return (
            <View style={styles.tab}>
                {routes && routes.map((route,index) => this.renderItem(route, index))}
            </View>
        );
    }
}
const styles = {
    tab:{
        borderTopWidth:StyleSheet.hairlineWidth,
        borderTopColor:Color.dddddd,
        width:WIDTH,
        height: Platform.OS==='ios'?SCALE(90):SCALE(100),
        backgroundColor:'white',
        flexDirection:'row',
        justifyContent:'space-around',
        alignItems:'center'
    },
    tabItem:{
        width:SCALE(100),
        alignItems:'center',
        justifyContent:'center'
    },
    tabText:{
        marginTop:SCALE(13),
        fontSize:FONT(10),
        color:Color.C7b7b7b
    },
    tabTextChoose:{
        color:Color.f3474b
    },
    tabImage:{
        width:SCALE(42),
        height:SCALE(42),
    },
}

然后一切都好了,世界安静了。
tabright.png

如果想实现凸起的效果请看TabNavigator自定义Tab(2)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值