微信小程序自定义tabbar
1. 微信小程序自定义的实现
参考官方文档
https://developers.weixin.qq.com/miniprogram/dev/framework/ability/custom-tabbar.html
- 配置信息
在 app.json 中的 tabBar 项指定 custom 字段,表示使用自定义的tabbar,同时其余 tabBar 相关配置也补充完整。所有 tab 页的 json 里需声明 usingComponents 项,也可以在 app.json 全局开启。
示例:
"tabBar": {
"custom": true,
"color": "#7A7E83",
"selectedColor": "#d81e06",
"borderStyle": "black",
"backgroundColor": "#ffffff",
"list": [{
"pagePath": "pages/home/home",
"iconPath": "images/home.png",
"selectedIconPath": "images/home-selected.png",
"text": "首页"
}, {
"pagePath": "pages/mine/mine",
"iconPath": "images/mine.png",
"selectedIconPath": "images/mine-selected.png",
"text": "我的"
}]
},
"usingComponents": {
"v-tabBar": "custom-tab-bar/index"
},
- 添加 tabBar 代码文件,在代码根目录下添加入口文件:
custom-tab-bar/index.js
custom-tab-bar/index.json
custom-tab-bar/index.wxml
custom-tab-bar/index.wxss
此文件可以在官方文档中导入代码片段获得,链接如下(https://developers.weixin.qq.com/s/jiSARvmF7i55)。
修改index.js里的list为自己的tab列表即可,注意为“/”目录
贴一下代码如下
custom-tab-bar/index.js
Component({
data: {
selected: 0,
"color": "#7A7E83",
"selectedColor": "#d81e06",
"list": [{
"pagePath": "/pages/home/home",
"iconPath": "/images/home.png",
"selectedIconPath": "/images/home-selected.png",
"text": "首页"
}, {
"pagePath": "/pages/mine/mine",
"iconPath": "/images/mine.png",
"selectedIconPath": "/images/mine-selected.png",
"text": "我的"
}]
},
attached() {
},
methods: {
switchTab(e) {
const data = e.currentTarget.dataset
const url = data.path
wx.switchTab({url})
this.setData({
selected: data.index
})
}
}
})
custom-tab-bar/index.json
{
"component": true
}
custom-tab-bar/index.wxml
<cover-view class="tab-bar">
<cover-view class="tab-bar-border"></cover-view>
<cover-view wx:for="{{list}}" wx:key="index" class="tab-bar-item" data-path="{{item.pagePath}}" data-index="{{index}}" bindtap="switchTab">
<cover-image class="aa" src="{{selected === index ? item.selectedIconPath : item.iconPath}}"></cover-image>
<cover-view class="bb" style="color: {{selected === index ? selectedColor : color}}">{{item.text}}</cover-view>
</cover-view>
</cover-view>
custom-tab-bar/index.wxss
.tab-bar {
position: fixed;
bottom: 0;
left: 0;
right: 0;
height: 48px;
background: white;
display: flex;
padding-bottom: env(safe-area-inset-bottom);
}
.tab-bar-border {
background-color: rgba(0, 0, 0, 0.33);
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 1px;
transform: scaleY(0.5);
}
.tab-bar-item {
flex: 1;
text-align: center;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
}
.tab-bar-item .aa {
width: 27px;
height: 27px;
}
.tab-bar-item .bb {
font-size: 10px;
}
- 编写 tabBar 代码
用自定义组件的方式编写即可,该自定义组件完全接管 tabBar 的渲染。另外,自定义组件新增 getTabBar 接口,可获取当前页面下的自定义 tabBar 组件实例。
在子tab页面的js文件中,把官方给的使用方法放到tabbar跳转页的onShow方法里,selected根据list下标位置进行设置
onShow: function () {
if (typeof this.getTabBar === 'function' &&
this.getTabBar()) {
this.getTabBar().setData({
selected: 0
})
}
},
2. 遇到的问题
1、模拟器运行结果一直报渲染层错误,如图所示
后来发现把调试基础库的版本改到了2.6.0就好了。
2.我实现的是登录后才进入首页和我的页面,一开始跳转用的navigateTo跳转页面custom-tab-bar/index,
wx.navigateTo({
url: '../../../custom-tab-bar/index',
}
但是跳进来首页内容不显示,需要点击tab项按钮才会显示,后来改为switchtab跳转到第一个tab项就好了
wx.switchTab({
url: '../../home/home',
})
成功实现,在这里又把调试基础库改回了2.12.2,并没有报错,说明就是我的子那个定义组件调用的问题,自己想想也是怎么可以跳到自定义组件页,就是从别的页面跳过来一定是到第一个tab项的页面。