效果:
说明:需要隐藏小程序自带的tabBar
下载地址:https://github.com/SuRuiGit/wxapp-customTabbar
使用步骤:
第一步:找到项目中的tabbarComponent目录,拷贝到你的工程中,然后将tabbarComponent->icon图标替换成你自己的tabbar图片,或者我们正常图片都是放在images目录下,这里我就是放在images下
第二步:到app.json中配置tabBar,只强调闲鱼的tabbar中间的按钮是跳到二级页面,所以不配置在tabBar的list中。如图所示,就是首页、名片、管理、我的配置在app.json中。
"tabBar": {
"backgroundColor": "#ffffff",
"color": "#979795",
"selectedColor": "#1c1c1b",
"list": [
{
"pagePath": "pages/index/index",
"iconPath": "images/icon/home.png",
"selectedIconPath": "images/icon/homeselect.png",
"text": "首页"
},
{
"pagePath": "pages/menu/menu",
"text": "名片",
"iconPath": "images/icon/menu.png",
"selectedIconPath": "images/icon/menuselect.png"
},
{
"pagePath": "pages/manager/manager",
"text": "管理",
"iconPath": "images/icon/message.png",
"selectedIconPath": "images/icon/messageselect.png"
},
{
"pagePath": "pages/person/person",
"iconPath": "images/icon/my.png",
"selectedIconPath": "images/icon/myselect.png",
"text": "我的"
}
]
}
第三步:在app.js的onLaunch方法中调用wx.hideTabBar();隐藏系统自带的tabBar
第四步:在app.js中的globalData中加入自定义tabbar的参数,再加入一个方法给tabBar.list配置中的页面使用,代码如下:
globalData: {
userInfo: null,
tabBar: {
"backgroundColor": "#ffffff",
"color": "#979795",
"selectedColor": "#1c1c1b",
"list": [
{
"pagePath": "/pages/index/index",
"text": "首页",
"iconPath": "/images/icon/home.png",
"selectedIconPath": "/images/icon/homeselect.png"
},
{
"pagePath": "/pages/menu/menu",
"text": "名片",
"iconPath": "/images/icon/menu.png",
"selectedIconPath": "/images/icon/menuselect.png"
},
{
"pagePath": "/pages/release/release",
"iconPath": "/images/icon/icon_release.png",
"isSpecial": true,
"text": "发布"
},
{
"pagePath": "/pages/manager/manager",
"text": "管理",
"iconPath": "/images/icon/message.png",
"selectedIconPath": "/images/icon/messageselect.png"
},
{
"pagePath": "/pages/person/person",
"text": "我的",
"iconPath": "/images/icon/my.png",
"selectedIconPath": "/images/icon/myselect.png"
}
]
}
},
editTabbar: function () {
//隐藏系统tabbar
wx.hideTabBar()
let tabbar = this.globalData.tabBar;
let currentPages = getCurrentPages();
let _this = currentPages[currentPages.length - 1];
let pagePath = _this.route;
(pagePath.indexOf('/') != 0) && (pagePath = '/' + pagePath);
for (let i in tabbar.list) {
tabbar.list[i].selected = false;
(tabbar.list[i].pagePath == pagePath) && (tabbar.list[i].selected = true);
}
_this.setData({
tabbar: tabbar
});
},
第五步:
在‘首页’、‘名片’、‘管理’、‘我的’页面的.json文件中分别加入 "usingComponents": { "tabbar": "../../tabbarComponent/tabbar" }
在.wxml文件中加入
,
在.js中data中加入tabbar:{}并onload方法中调用app.editTabbar();