如果應用是一個多 tab 應用,可以通過 tabBar 配置項指定一級導航欄,以及 tab 切換時顯示的對應頁。
在 pages.json 中提供 tabBar 配置,不僅僅是為了方便快速開發導航,更重要的是在App和小程序端提升性能。在這兩個平臺,底層原生引擎在啟動時無需等待js引擎初始化,即可直接讀取 pages.json 中配置的 tabBar 信息,渲染原生tab。
Tips
- 當設置 position 為 top 時,將不會顯示 icon
- tabBar 中的 list 是一個數組,最少要配置2個、最多只能配置5個 tab,tab 按數組的順序排序。
- tabbar 切換第一次加載時可能渲染不及時,可以在每個tabbar頁面的onLoad生命周期里先彈出一個等待雪花(hello uni-app使用了此方式)
- tabbar 的頁面展現過一次后就保留在內存中,再次切換 tabbar 頁面,只會觸發每個頁面的onShow,不會再觸發onLoad。
- 頂部的 tabbar 目前僅微信小程序上支持。需要用到頂部選項卡的話,建議不使用 tabbar 的頂部設置,而是自己做頂部選項卡,可參考 hello uni-app->模板->頂部選項卡。
屬性說明:
屬性 | 類型 | 必填 | 默認值 | 描述 | 平臺差異說明 |
---|---|---|---|---|---|
color | HexColor | 是 | tab 上的文字默認顏色 | ||
selectedColor | HexColor | 是 | tab 上的文字選中時的顏色 | ||
backgroundColor | HexColor | 是 | tab 的背景色 | ||
borderStyle | String | 否 | black | tabbar 上邊框的顏色,可選值 black/white,black對應顏色rgba(0,0,0,0.33),white對應顏色rgba(255,255,255,0.33)。 | App 2.3.4+ 、H5 3.0.0+ |
list | Array | 是 | tab 的列表,詳見 list 屬性說明,最少2個、最多5個 tab |
其中 list 接收一個數組,數組中的每個項都是一個對象,其屬性值如下:
屬性 | 類型 | 必填 | 說明 | 平臺差異 |
---|---|---|---|---|
pagePath | String | 是 | 頁面路徑,必須在 pages 中先定義 | |
text | String | 是 | tab 上按鈕文字,在 App 和 H5 平臺為非必填。例如中間可放一個沒有文字的+號圖標 | |
iconPath | String | 否 | 圖片路徑,icon 大小限制為40kb,建議尺寸為 81px * 81px,當 position 為 top 時,此參數無效,不支持網絡圖片,不支持字體圖標 | |
selectedIconPath | String | 否 | 選中時的圖片路徑,icon 大小限制為40kb,建議尺寸為 81px * 81px ,當 position 為 top 時,此參數無效 | |
visible | Boolean | 否 | 該項是否顯示,默認顯示 | App (3.2.10+)、H5 (3.2.10+) |
iconfont | Object | 否 | 字體圖標,優先級高于 iconPath | App(3.4.4+)、H5 (3.5.3+) |
tarBar寫于page.json中,與pages,globalStyle并列
示例代碼:
"tabBar": {"color": "#999","selectedColor": "#2B9939","borderStyle": "white","list": [{"pagePath" : "pages/index/index","text": "首頁","iconPath": "/static/images/tabBar/home.png","selectedIconPath": "/static/images/tabBar/home-h.png"},{"pagePath" : "pages/classfy/classfy","text": "分類","iconPath": "/static/images/tabBar/classify.png","selectedIconPath": "/static/images/tabBar/classify-h.png"},{"pagePath" : "pages/user/user","text": "我的","iconPath": "/static/images/tabBar/user.png","selectedIconPath": "/static/images/tabBar/user-h.png"}]}
?效果: