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参数。
最终效果如下:
热门推荐
港股的买卖规则如何深入理解?这种理解对投资有何帮助?
碘酊和碘伏的区别是什么
前端框架新势力大盘点
银行储蓄卡转账限额全攻略:限额标准与调整方法详解
内蒙古博物院:讲述北疆交往交流交融故事
玉米须的营养价值
雷神之锤(Mjolnir)与赛博锤:神话与现代的交汇
安全与效率并重,共享电摩电池的控制之道
“异地购车”,究竟是省钱秘籍,还是坑?
准爸爸如何学习怀孕和育儿知识?
临产在即,准爸爸“抄作业”攻略:孕晚期如何支持妈妈和宝宝
如何设计一个吸引人的高效沟通宣传口号?
工厂安全如何管理
WAMP环境端口占用问题解决方案
六旋翼无人机设计:从结构到飞行控制的全面解析
百公斤大载重六旋翼吊运无人机技术详解
长焦镜头应该选多少毫米 长焦镜头怎么选择
码农到架构师如何成长
“负电价”是什么概念,对未来的电力系统发展有何影响?
未签订合同先开工合法吗
吕布:武力与智谋的辨析
风管机如何清洗?这6个风管机清洗环节要了解
怎样使用AI高效撰写文章,提升创作效率与质量?
什么是高绩效教练的核心理念?
团队压力大如何激励
股东动态管理是什么
父亲是否应当承担教育责任?
产学研项目如何管理
ACM-ICPC数学数论:连分数详解
哪里可以找到行业市场分析的经典案例?