在開發小程序過程當中,相信有一部分人,遇到過一個問題:當使用tabBar跳轉頁面時,所跳轉的頁面下方一定有 tabBar顯示,而當你須要把它隱藏時,卻一籌莫展。話很少說,在這裏給你們分享如何隱藏tabBar的方法。json
使用自定義tabBar,新建一個tarBar.wxml模板頁,而後引用模板的頁面傳入數據便可,代碼以下:小程序
<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>
接下來是在index.js的配置對象:app
tabBar:{ "color": "#9E9E9E", "selectedColor": "#f00", "backgroundColor": "#fff", "borderStyle": "#ccc", "list":[{ "pagePath": "/pages/index/index", "text": "主頁", "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表示該頁面是否被選中,pageTum表示點擊該頁面跳轉方式,‘其餘’這個頁面不用設置tabBar,而且它的pageTum的值是navigate,表示點擊‘其餘’跳轉的頁面就不會顯示tabBar。函數
index.wxml引入模板:flex
<import src="../template/tabBar.wxml" /> <template is="tabBar" data="{{tabBar: tabBar}}" /> <text>主頁面</text> //顯示內容
而後在mine頁面也同樣配置數據把active的值改成true,引入模板。效果以下:this
使用原生tabBar跳轉至一級頁面,再利用周期函數onShow的特性直接跳轉到咱們須要看到的頁面,而且在返回時使用wx.swicthTab跳轉至程序設計所需的一級頁面。下面來看一看實現方法:url
首先在app.json中設置tabBarspa
"tabBar": { "color": "#9E9E9E", "selectedColor": "#f00", "backgroundColor": "#fff", "borderStyle": "#ccc", "list": [{ "pagePath": "pages/index/index", "text": "主頁", "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" } ] }
在‘其餘’這個頁面中設置跳轉頁面爲一箇中間過渡頁面pageTum,而後利用pageTum的周期函數onShow跳轉至無tabBar的二級頁面tum,返回時就能直接返回至主頁面,代碼以下:設計
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' }) } }
實現效果code
若是有錯誤或者其餘的方法,但願能夠指出和交流,謝謝!