uniapp自定义底部TabBar并使用uni.switchTab的完整教程
创作时间:
作者:
@小白创作中心
uniapp自定义底部TabBar并使用uni.switchTab的完整教程
引用
CSDN
1.
https://blog.csdn.net/m0_65629667/article/details/139021324
在uniapp开发中,自定义底部TabBar是一个常见的需求。本文将详细介绍如何在保持
uni.switchTab功能的同时,实现自定义TabBar的开发。
1. 隐藏系统TabBar
首先需要在pages.json文件中配置系统TabBar的高度为0,使其不再显示。同时,需要将自定义TabBar所需的页面添加到配置中。
2. 创建自定义TabBar组件
建议在项目中创建一个components文件夹来存放自定义组件。然后在该文件夹下新建一个TabBar文件,可以选择使用scss、less或css来编写样式。
index.vue
<template>
<view style="height: 180rpx;">
<view class="content" :style="{zIndex}">
<view class="tabber_box">
<view v-for="(item,index) in tabBars" :key="index"
:style="{width:`${ratio}%`,display:'flex',justifyContent:'space-around',margin: '24rpx 0'}">
<view class="tabber_item" @click="onNav(item.pagePath)">
<image v-if="currentPath === item.pagePath" :src="item.iconPath"></image>
<image v-else :src="item.selectedIconPath"></image>
<text
:style="{color:currentPath === item.pagePath ? item.selectedColor : item.color}">{{item.name}}</text>
</view>
</view>
</view>
</view>
</view>
</template>
<script>
export default {
props: {
currentPath: { // 当前页面路径
type: String,
default: '/pages/index/index',
},
zIndex: { // 界面层级
type: Number,
default: 10
}
},
data() {
const color = '#FFFFFF66';
const selectedColor = '#FFF';
return {
tabBars: [{
name: '首页',
iconPath: '../../static/tabbar/index.png',
selectedIconPath: '../../static/tabbar/selectd_index.png',
pagePath: '/pages/index/index',
color,
selectedColor,
},
{
name: '邀请',
iconPath: '../../static/tabbar/lnvite.png',
selectedIconPath: '../../static/tabbar/selectd_lnvite.png',
pagePath: '/pages/lnvite/index',
color,
selectedColor,
},
{
name: 'VIP',
iconPath: '../../static/tabbar/vip.png',
selectedIconPath: '../../static/tabbar/selectd_vip.png',
pagePath: '/pages/vip/index',
color,
selectedColor,
},
{
name: '我的',
iconPath: '../../static/tabbar/my.png',
selectedIconPath: '../../static/tabbar/selectd_my.png',
pagePath: '/pages/my/index',
color,
selectedColor,
},
],
ratio: 0,
isLogin: false,
}
},
mounted() {
this.ratio = 100 / this.tabBars.length;
},
methods: {
onNav(url) {
if (this.currentPath !== url) uni.switchTab({
url
});
}
}
}
</script>
<style lang="scss" scoped>
@import './index.scss';
</style>
index.scss
.content {
position: fixed;
bottom: 0;
width: 100%;
.tabber_box {
display: flex;
flex-direction: row;
align-items: center;
background: #151d33;
padding-bottom: calc(env(safe-area-inset-bottom) - 48rpx) ; // 适配iphoneX的底部
padding-bottom: calc(env(safe-area-inset-bottom) - 48rpx); /*兼容 IOS>11.2*/
image {
width: 56rpx;
height: 56rpx;
// margin-bottom: 16rpx;
}
.tabber_item {
display: flex;
flex-direction: column;
align-items: center;
font-size: 28rpx;
}
}
}
3. 注册全局组件
在main.js或main.ts文件中,将创建好的TabBar组件注册为全局组件。
4. 使用自定义TabBar
在需要显示TabBar的页面中引入组件,并根据当前页面路径设置currentPath参数。
最终效果如下:
热门推荐
刘勰笔下的“渺邈”,竟是这般神奇?
《生死狙击》新手速成秘籍:从基础操作到实战技巧全解析
继续教育选哪种?成考自考区别全解析
从卖萌到道歉:6种实用技巧化解女友怒气
掌握关键技巧,轻松解决豆腐脑凝固难题
一文掌握手写风格字体设计:3个案例详解
陈宝国儿子陈月末:被父亲捧了12年,凭借《珠江人家》逆风翻盘
胃在求救:这些症状别轻易说“不”
智能加热工作服PK石墨烯保暖内衣,谁更适合极寒环境?
尿频尿急尿痛?夏季尿路感染高发,这样做能预防
宏观经济因素如何影响股市波动?
别让不锈钢厨具生锈:从预防到处理的全方位指南
饮食运动双管齐下,远离肠梗阻困扰
西洋参枸杞泡水:养生界的黄金搭档,轻松喝出好气色
云南腾冲:中缅边境的十大宝藏地,从古镇到火山一网打尽
西梅干的健康吃法:从传统到创新,打造你的地中海饮食新选择
春节送礼首选:朱砂手链
如何确保公司遵守请休假管理制度?
系统构建沟通反馈机制,全方位提升小学教学质量
冬季驾驶:如何省油又保暖?
饭后立即洗澡,小心心血管“抗议”
从20天到3次:蛇的蜕皮周期与影响因素全解析
新学期营养指南:家长必知的学生营养餐五大原则
Pi币牵手四大行?官方回应:并无此事
南宁爱尔眼科医院:常见眼部异常解析
【打击整治网络谣言】如何辨别网络谣言,成为2025年的信息智者?
梁武帝四次“舍身”地,鸡鸣寺见证佛教文化兴衰
战略管长远,战术看眼前:企业目标设定指南
癌症患者饮食指南:从食材到食谱的全方位建议
年检虽烦,确保行车安全,不容忽视