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进行页面切换。
热门推荐
孩子脸上出现一块白什么原因造成的
巴特勒稳定carry,维金斯与李凯尔状态低迷
历史与虚构:李世民与李元霸的真实关系
足弓疼是怎么回事
OBD-II故障码全解析:从零开始的解读指南(附实际操作)
汽车故障诊断仪使用教程:从设备连接到故障排查
1949-2024年中国出生人口数据统计:从1275万到954万的变迁
驾驶证到期可以跨省异地换证吗
番茄种植全攻略:从环境要求到病虫害防治
【2025哈尔滨旅游攻略】一文掌握哈尔滨最佳旅游方案「附热门路线」
如何高效执行目标任务管理的步骤?
人民日报:银发力量释放人口新红利
组合贷款商贷还清剩公积金部分还能抵个税吗?
科目一到科目四多少分及格
皮肤暗黄怎么食补调理
征信申请异议成功的几个条件,征信提交异议申请多长时间能消除
家用燃气热水器如何选购?这8点一定要知道!
辣的胃疼怎么办
中国“人造太阳”实现1066秒高约束模运行,刷新世界纪录
仙境张家界的"治愈"密码——从讲解员阿见走红看一座旅游城市的底蕴
离婚财产分割中如何选择合适的律师:关键步骤与注意事项
味精、鸡精都是“害人精”?吃得越多癌来越快?
场外期权的交易商有哪些?
微星笔记本屏幕教程:从亮度调节到色彩校准,掌握屏幕调整技巧
考研不确定考什么专业怎么办,五步帮你找到适合的研究方向
软件优化:提升用户体验的关键策略
如何起诉变更子女姓名?流程图/办理指南!
玻璃水,你真的了解吗?
移动端表格列表页设计:8种实用解决方案
低温工业冷冻机制冷剂泄露的检查与维修指南