按照官方自定義tabBar:
- 配置信息
在 app.json 中的 tabBar 項(xiàng)指定 custom 字段,同時(shí)其余 tabBar 相關(guān)配置也補(bǔ)充完整。
所有 tab 頁(yè)的 json 里需聲明 usingComponents 項(xiàng),也可以在 app.json 全局開(kāi)啟。
我的示例:
{
"tabBar": {
"custom": true,
"color": "#a9b7b7",
"selectedColor": "#11cd6e",
"borderStyle": "black",
"list": [
{
"pagePath": "pages/index/index",
"iconPath": "images/tabbar/home0.png",
"selectedIconPath": "images/tabbar/home1.png",
"text": "首頁(yè)"
},
{
"pagePath": "pages/my/index",
"iconPath": "images/tabbar/my0.png",
"selectedIconPath": "images/tabbar/my1.png",
"text": "我的"
}
]
-
添加 tabBar 代碼文件
在代碼根目錄下添加入口文件 custom-tab-bar :必須與pages在同一根目錄下。 -
編寫(xiě) tabBar 代碼
用自定義組件的方式編寫(xiě)即可,該自定義組件完全接管 tabBar 的渲染,我的代碼如下:
index.js:
// custom-tab-bar/index.js
Component({
data: {
selected: 0,
color: "#696969",
selectedColor: "#31F60A",
"list": [
{
"pagePath": "/pages/index/index",
"iconPath": "/images/tabbar/home0.png",
"selectedIconPath": "/images/tabbar/home1.png",
"text": "首頁(yè)",
},
{
"pagePath": "/pages/my/index",
"iconPath": "/images/tabbar/my0.png",
"selectedIconPath": "/images/tabbar/my1.png",
"text": "我的",
}
]
},
attached() {
},
methods: {
switchTab(e)
{
const data = e.currentTarget.dataset
const url = data.path
wx.switchTab({url})
// this.setData({
// selected: data.index
// })
}
}
})
index.json:
{
"component": true
}
index.wxml:
<!--miniprogram/custom-tab-bar/index.wxml-->
<view class="tab-bar">
<view class="tab-bar-border"></view>
<view wx:for="{{list}}" wx:key="index" class="tab-bar-item" data-path="{{item.pagePath}}" data-index="{{index}}" bindtap="switchTab">
<image src="{{selected === index ? item.selectedIconPath : item.iconPath}}"></image>
<view style="color: {{selected === index ? selectedColor : color}}">{{item.text}}</view>
</view>
</view>
index.wxss:
/* 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 image {
width: 27px;
height: 27px;
}
.tab-bar-item view {
font-size: 10px;
}
4.在每一個(gè)需要引用自定義tabBar的頁(yè)面js文件內(nèi)添加引入代碼:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-623756.html
// 引入Tabbar
Component({
pageLifetimes: {
show() {
if (typeof this.getTabBar === 'function' &&
this.getTabBar()) {
this.getTabBar().setData({
selected: 4
})
}
}
}
})
這里的 selected:首頁(yè)是0,之后依次排序。
好了,到這里基本是官方給出的方法,但是首次點(diǎn)擊tabBar時(shí),圖標(biāo)閃爍,試驗(yàn)了很多,都無(wú)效。
最后將app.json內(nèi)引入tabBar組件的 custom由true改為false,就不再閃爍了。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-623756.html
到了這里,關(guān)于解決微信小程序 自定義tabBar 首次切換時(shí)候閃爍問(wèn)題(實(shí)測(cè))的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!