在開發(fā)小程序過(guò)程中,相信有一部分人,遇到過(guò)一個(gè)問(wèn)題:當(dāng)使用tabBar跳轉(zhuǎn)頁(yè)面時(shí),所跳轉(zhuǎn)的頁(yè)面下方必定有 tabBar顯示,而當(dāng)你需要把它隱藏時(shí),卻束手無(wú)策。話不多說(shuō),在這里給大家分享如何隱藏tabBar的方法。
方法一:自定義tabBar
使用自定義tabBar,新建一個(gè)tarBar.wxml模板頁(yè),然后引用模板的頁(yè)面?zhèn)魅霐?shù)據(jù)即可,代碼如下:
<template name="tabBar"> <view class="flex-h" style="color: {{tabBar.color}}; background: {{tabBar.backgroundColor}}; {{tabBar.position=='top'? 'top: 0' : 'bottom: 0'}}; {{tabBar.borderStyle? (tabBar.position=='top'? 'border-bottom: solid 1px '+tabBar.borderStyle + ';' : 'border-top: solid 1px '+tabBar.borderStyle + ';') : ''}}"> <block wx:for="{{tabBar.list}}" wx:key="pagePath"> <navigator url="{{item.pagePath}}" open-type="{{item.pageTum}}" class="menu-item" style="{{item.active? 'color: '+(item.selectedColor? item.selectedColor : tabBar.selectedColor) : ''}}"> <image src="{{item.selectedIconPath}}" wx:if="{{item.active}}"></image> <image src="{{item.iconPath}}" wx:if="{{!item.active}}"></image> <text>{{item.text}}</text> </navigator> </block> </view> </template>
接下來(lái)是在index.js的配置對(duì)象:
tabBar:{ "color": "#9E9E9E", "selectedColor": "#f00", "backgroundColor": "#fff", "borderStyle": "#ccc", "list":[{ "pagePath": "/pages/index/index", "text": "主頁(yè)", "iconPath": "../../images/index.png", "selectedIconPath": "../../images/index_active.png", "pageTum": "redirect", "selectedColor": "#4EDF80", active: true }, { "pagePath": "/pages/tum/tum", "text": "其他", "iconPath": "../../images/pageTum.png", "pageTum": "navigate", "selectedColor": "#4EDF80", active: false }, { "pagePath": "/pages/mine/mine", "text": "我的", "iconPath": "../../images/mine.png", "selectedIconPath": "../../images/mine_active.png", "pageTum": "redirect", "selectedColor": "#4EDF80", active: false }], "position": "bottom" } }
在這里要注意的是,active表示該頁(yè)面是否被選中,pageTum表示點(diǎn)擊該頁(yè)面跳轉(zhuǎn)方式,‘其他’這個(gè)頁(yè)面不用設(shè)置tabBar,并且它的pageTum的值是navigate,表示點(diǎn)擊‘其他’跳轉(zhuǎn)的頁(yè)面就不會(huì)顯示tabBar。
index.wxml引入模板:
<import src="../template/tabBar.wxml" /> <template is="tabBar" data="{{tabBar: tabBar}}" /> <text>主頁(yè)面</text> //顯示內(nèi)容
然后在mine頁(yè)面也一樣配置數(shù)據(jù)把a(bǔ)ctive的值改為true,引入模板。效果如下:

方法二:使用中間頁(yè)面跳轉(zhuǎn)
使用原生tabBar跳轉(zhuǎn)至一級(jí)頁(yè)面,再利用周期函數(shù)onShow的特性直接跳轉(zhuǎn)到我們需要看到的頁(yè)面,并且在返回時(shí)使用wx.swicthTab跳轉(zhuǎn)至程序設(shè)計(jì)所需的一級(jí)頁(yè)面。下面來(lái)看一看實(shí)現(xiàn)方法:
首先在app.json中設(shè)置tabBar
"tabBar": { "color": "#9E9E9E", "selectedColor": "#f00", "backgroundColor": "#fff", "borderStyle": "#ccc", "list": [{ "pagePath": "pages/index/index", "text": "主頁(yè)", "iconPath": "images/index.png", "selectedIconPath": "images/index_active.png" }, { "pagePath": "pages/tum/pageTum", "text": "其他", "iconPath": "images/pageTum.png" }, { "pagePath": "pages/mine/mine", "text": "我的", "iconPath": "images/mine.png", "selectedIconPath": "images/mine_active.png" } ] }
在‘其他’這個(gè)頁(yè)面中設(shè)置跳轉(zhuǎn)頁(yè)面為一個(gè)中間過(guò)渡頁(yè)面pageTum,然后利用pageTum的周期函數(shù)onShow跳轉(zhuǎn)至無(wú)tabBar的二級(jí)頁(yè)面tum,返回時(shí)就能直接返回至主頁(yè)面,代碼如下:
data: { num: 0, }, onLoad: function() {}, onShow: function() { this.data.num++; if (this.data.num % 2 == 0) { wx.switchTab({ url: '../index/index' }); } else { wx.navigateTo({ url: './tum' }) } }
實(shí)現(xiàn)效果

如果有錯(cuò)誤或者其他的方法,希望可以指出和交流,謝謝!