UniApp自定义底部TabBar并使用uni.switchTab功能
创作时间:
作者:
@小白创作中心
UniApp自定义底部TabBar并使用uni.switchTab功能
引用
CSDN
1.
https://blog.csdn.net/m0_65629667/article/details/139021324
在UniApp开发中,自定义底部TabBar是一个常见的需求。本文将详细介绍如何在UniApp中实现自定义TabBar,并确保能够使用uni.switchTab功能进行页面切换。
1. 隐藏原生TabBar
首先需要在pages.json文件中配置原生TabBar的height属性为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>
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. 使用方法
假设当前页面路径为/pages/index/index,则在该页面引入并使用自定义TabBar组件,同时将currentPath参数设置为当前页面路径。其他页面同理。
最终效果
通过以上步骤,你将能够实现一个功能完备的自定义TabBar,并且可以使用uni.switchTab进行页面切换。
热门推荐
深挖86版《西游记》:从幕后资助到现实中的“西游之路”
游戏开发怎么找美术资源——全面解析资源获取之道
《战地》系列游戏简史回顾
北魏名臣崔浩:冤案背后的忠诚与悲剧
湖北十堰彩礼习俗:传统与现实的交织
足底筋膜炎舒緩與預防方法,物理治療師不藏私通通教你
2025高考蓝皮书 | 高考英语关键能力的培养与应用
敦煌美食极品——泡儿油糕
油画课程油画风景写生课件
中外AI的第一次对决,是DeepSeek的抽象级完胜
附子理中丸的副作用解析与应对策略
阮经天给当下演艺圈带来哪些启示
四川南充多地正月十四“蛴蟆节”:非遗民俗的年味新狂欢
2024ESI年度榜单出炉:中国高校全球影响力持续提升
毛坯房和清水房的区别及注意事项
近视不是简单的"看不清",青少年做好预防,远离4个视力陷阱
“四为”引领,异构绽放:眉县中学探索课堂教学新范式
从化疗到死亡仅一年?肿瘤医生提醒:这6种癌症患者不宜化疗
菊花烘干机在香菇领域的应用:质托未来!
嵌入式系统:从基础到未来发展,你了解多少?
章丘,“百强”!
超4600只个股上涨!创业板指涨超2%,人形机器人概念股爆发
ASIL详解:汽车功能安全的关键指标
美联储加息对中国对外贸易环境变化的影响及其应对策略
集安高句丽壁画:穿越千年的艺术瑰宝
脖子、腋窝长这2个东西,提示你可能得了糖尿病,赶紧对照看
中医治疗抑郁症的方法全解析
八字“比劫”过旺,到底用“官杀”制好,还是用“食伤”泄好?
绍兴出发:乘坐各类交通工具前往大理完整指南
鼻炎堵塞晚上睡不着的原因及治疗方法