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参数。
最终效果如下:
热门推荐
真正优秀的家庭:始于陪伴,陷于教育,忠于三观
中亚有哪些古老仪式与传统?
联合国前副秘书长索尔海姆:中国是全球应对气候变化行动中不可或缺的国家
房间床头朝向什么方向最好?家居风水与健康的关系
怎样提醒老板发工资不尴尬?这份职场指南请收好
厨房瓷砖选购全攻略:从颜色到规格的五大要点
从计算机跨界生物学,坐了10年冷板凳后,他开创了蛋白质预测新范式
多家贷款逾期怎么办
灵活就业人员医保选择指南:职工医保与居民医保的权衡
孙权一生之耻,十万惨败八百,逍遥津之战的军事智慧与战略转折
残疾人音乐疗法:从情感表达到康复实践
全球粮食产量过去60年稳步增长
电动车转把故障怎么修?电动车把手失灵可以自己修吗?
谁最先说的印欧语?DNA指向6400年前的欧亚牧民
感冒了怎么办?常备哪些药?
既然转基因添加剂食品对人体没有伤害,为什么会有“外国人明令禁止”的说法?
攻克癌症新视角:中山大学符立梧团队革新循环肿瘤细胞类器官培养技术
二甲基亚砜中间体在制药领域的应用
心房颤动的治疗方法与注意事项
痛风的治疗方案探讨
"开盒事件"深度解析:数据安全、家庭教育与技术伦理的多重挑战
自制有机肥的5种方法
躯体化症状:原因、症状与治疗方法全解析
今年年花普遍质优价平 石马桃花价格略涨一成
倒车雷达的使用技巧是什么?如何正确安装和调试倒车雷达?
亚冠焦点战前瞻:神户胜利船vs上海海港!海港能否守住中超荣耀?
一位非985/211毕业生的逆袭之路
传统企业电商转型指南:如何提升竞争力?
建筑工程技术专业就业前景怎么样 2025好找工作吗
学生物品管理安全教育