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
参数。
最终效果如下:
热门推荐
“千古第一谋臣”刘伯温最经典的4句箴言,句句智慧深邃,读后受益匪浅!
汽车智能制造,革命性的新技术将如何改变未来的汽车行业
卫生间安装止逆阀的注意事项及作用
垂体危象怎么预防
医院医学影像人工智能辅助诊断系统:智慧医疗的新助手
解密射频技术:频段、传播方式与无线通信的核心奥秘
从性能到耐用性,如何选购高性价比的Type-C数据线?
寒假适合1-6年级学生看的电影,对孩子是最好的精神滋养
12生肖排行顺序顺口溜!这样背生肖顺序,孩子3分钟倒背如流
十二生肖正确排序表口诀(记忆快速排序)
PM项目管理中如何高效查找文件
Markdown与LaTeX结合使用指南:打造高效学术写作流程
每天早上恶心干呕怎么办好
C1驾驶证增驾B2需要什么条件?车管所:满足这4个要求就行!
便利店投资回报率怎么算最准确?
债市波动加剧,长短端利率倒挂下长债遭抛售
数字档案馆关键技术大揭秘|应用场景与技术对策
餐桌尺寸一般是多少?餐桌尺寸选择与家庭用餐空间布局
早上空腹吃藕粉好吗?医生的专业解答来了
搜遍全网,总结出最适合女生做10大副业,快来看看吧
城中村“电费刺客”调查:有租客称电费比房租还贵,专家建议推进一户一表改造
14年德国夺冠首发阵容今何在?仅5人在役,队长夺冠后33岁就退役
年纪轻轻就长白发?一截黑、一截白?当心!或是身体警告!
你真的知道如何选择数据线吗?从快充、数据传输到材质长度,一篇文章告诉你
世界首创!二氧化碳加氢制汽油,为啥没推广开来?
家族性遗传肿瘤---肾癌篇
早期发现肾癌的三种有效方法
昆明盘龙江硅藻研究:基于飞纳台式扫描电镜的硅藻全自动检测系统
总有一款适合!从新手到专家:十大电气制图软件全解析
什么是私家车?从定义到保养全解析