uni-app 自定义tab-bar

前言

uni-app官方的tabBar修改字体大小和图片大小不生效,并且有闪烁的问题。自定义tab-bar可以解决这些问题


一、自定义tab-bar.vue

<template>
	<view class="uni-tabbar">
		<view class="uni-tabbar__item" v-for="(item,index) in tabbar" :key="index" @tap="changeTab(item)">
			<uni-icons custom-prefix="iconfont" :type="item.fontIcon"  :color="item.pagePath == pagePath?'#C80808':'#979797'"></uni-icons>
			<view class="uni-tabbar__label" :class="{'active': item.pagePath == pagePath}">
				{{item.text}}
			</view>
		</view>
	</view>
</template>

<script>
    export default {
        props: {
            pagePath: null
        },
        data() {
            return {
                page: 'contact',
                showPage: false,
                containerHeight: 400,
                tabbar: [
                    {
                    	"pagePath": "/pages/index/index",
						"fontIcon": "icon-home",
                    	"text": "首页"
                    },
                    {
                    	"pagePath": "/pages/order/list",
						"fontIcon": "icon-order",
                    	"text": "订单"
                    },
                    {
                    	"pagePath": "/pages/product/index",
						"fontIcon": "icon-product",
                    	"text": "产品"
                    },

                    {
                    	"pagePath": "/pages/personal/index",
						"fontIcon": "icon-center",
                    	"text": "我的"
                    }
                ]
            };
        },
        mounted() {
            
        },
        methods: {
            changeTab(item) {
                this.page = item.pagePath;
                uni.switchTab({
                    url: this.page
                });
				uni.hideTabBar({
				    animation: false
				})
            },
        }
    }
</script>

<style lang="scss" scoped>
    .uni-tabbar {
        position: fixed;
        bottom: 0;
        //z-index: 900;
        width: 100%;
        display: flex;
        justify-content: space-around;
        //height: 98upx;
        padding: 14px 0;
        box-sizing: border-box;
        border-top: solid 1upx #ccc;
        background-color: #fff;
        box-shadow: 0px 0px 17upx 1upx rgba(206, 206, 206, 0.32);
        .uni-tabbar__item {
            display: block;
            font-size: 12px;
            text-align: center;
			.uni-active {
				
			}
        }
        .uni-tabbar__label {
            font-size: 12px;
            color: #979797;
            &.active {
                color: #C80808;
            }
        }
		.uni-active {
			color: #C80808;
		}
    }
</style>

二、main.js 全局引入tab-bar.vue

import tabBar from '@/components/tab-bar.vue'

Vue.component('tabBar', tabBar)

三、pages.json 添加tabBar配置

"tabBar": {
		"selectedColor":"#79D5AD",
		"color": "#999999",
		"backgroundColor":"#ffffff",
		"borderStyle": "white",
		"list": [{
		    "pagePath":"pages/index/index",
		    "text": " "
		},{
		    "pagePath":"pages/order/list",
		    "text": " "
		},{
		    "pagePath":"pages/product/index",
		    "text": " "
		},{
		    "pagePath":"pages/personal/index",
		    "text": " "
		}]
	}

三、其他需要显示tabBar的页面引入第一步中的tab-bar组件

<tabBar :pagePath="'/pages/index/index'"></tabBar>

隐藏默认的tar-bar

onShow() {
    uni.hideTabBar({
		animation: false
	})
}

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

时光太浅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值