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参数。
最终效果如下:
热门推荐
如何科学修炼"三魂七魄"?
戛洒平寨社区的适老性公共文化空间改造设计
提门槛、降收入,番茄小说对网文作者不再客气
公务员行测考试时间不够用?这份应对指南请收好
从眉山市出发抵达成都的通勤时间解析
域名解析常见的问题有哪些?
为何碎片化睡眠危害等同于熬夜?什么是碎片化睡眠?
钉钉如何接API
如何写好AI绘画提示词?保姆级教程来了!
Excel计算几天后日期的多种方法
纽大教授:手机里泡大的孩子们,终于熬成了“焦虑的一代”
头孢拉定胶囊一次吃几粒成人
庚午日柱是什么命 庚午日柱对命主的影响分析
孩子冲突不断?家长应对有招!
股票如何卖出?卖出股票的时机如何把握?
家常荠菜饺子制作教程:从选材到出锅的完整指南
AI人工智能专业都开设了哪些课程
什么是案例分析法
五心烦热手脚热的食疗调理方法
节气文韵主题日历 诗歌里的大自然:寒露
杏的营养价值及功效
知道短视频怎么搜电影
四折一楔子:元杂剧剧本体制的灵活性与多样性
如何与领导建立互信的工作关系
揭秘珠海日月贝:海岛剧院的硬核秘密
中国本科率:本科率提升的原因和影响
电动机星形/三角降压启动电路图接线原理
新高度!六支战队齐聚ALGS季后赛,为荣誉而战!
用GPT总结2024年的人,全破防了
酒精催化暴力?为何醉酒让人更易发怒?