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进行页面切换。
热门推荐
高考145分学霸分享:用睡眠记忆法搞定高一词汇
甲午战争中的旅顺保卫战:一场本可避免的悲剧
鸡兔同笼:一道古代数学题的现代启示
自嗨锅:户外旅行必备神器!
自热锅正确使用全攻略:从安全到美味的全方位指南
自热火锅:方便快捷还是营养陷阱?
自热锅频现安全事故,这些使用禁忌你必须知道
揭秘军校招生体检标准:那些你必须知道的细节
一模成绩如何换算全省位次?这份实用指南请收好
节后焦虑?廖春岭教你快速恢复状态!
复旦专家推荐:节后饮食调整指南
喝牛奶真的能养胃吗?真相揭秘!
慢性非萎缩性胃炎的调理方法:从药物到生活全攻略
普洱茶,非萎缩性胃炎患者的养胃之选
JCI认证视角下:院感系统如何提升医院感染防控水平
腊梅的芳香
自热火锅安全使用指南:小心别让美食变“炸弹”!
自热锅真的不健康?真相揭秘!
自嗨锅使用指南:保姆级教程,安全又美味!
糖尿病和高血压:肾功能损害的“沉默杀手”
春节返岗必读:7个实用技巧助你快速进入工作状态
磁器口美食有哪些,磁器口美食好吃吗,磁器口美食推荐
四月重庆磁器口探寻地道小吃之旅
梁文峰:从量化投资到AI大模型,一位技术理想主义者的征途
梁文峰:从量化投资到AI传奇
小年夜读古诗,品味浓浓年味
小年夜祭灶王,糖瓜粘住好运!
小冰期:明朝末年的气候灾难
辽东防线揭秘:明末军事力量大比拼
崇祯帝的治国新招:君臣面对面