很簡單 TabBar是一個對象,然后項存在list中,list是和 對象,最少2個項 最多5個:

/*tabBar是一個對象*/ "tabBar":{ "color":"#ff55ff", //tabBar 字體顏色 "selectedColor":"#007AFF", //tabBar 被選擇選擇顏色 "borderStyle":"black", //tabBar 上邊框顏色 "backgroundColor":"#ffffff", //tabBar背景顏色 /* 最少2個 最多5個 導航項*/ "list":[ { //注意pagePath 路勁要和上面的包路勁一樣 "pagePath":"pages/index/index", "text":"首頁", "iconPath":"static/logo/home.png", "selectedIconPath":"static/logo/homeA.png" }, { "pagePath":"pages/search/search", "text":"搜索", "iconPath":"static/logo/search.png", "selectedIconPath":"static/logo/searchA.png" }, { "pagePath":"pages/me/me", "text":"我的", "iconPath":"static/logo/me.png", "selectedIconPath":"static/logo/meA.png" } ] }
放在pages.json中
效果:
官方說:
如果應用是一個多 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 | App 2.3.4+ 支持其他顏色值、H5 3.0.0+ |
blurEffect | String | 否 | none | iOS 高斯模糊效果,可選值 dark/extralight/light/none(參考:使用說明) | App 2.4.0+ 支持、H5 3.0.0+(只有最新版瀏覽器才支持) |
list | Array | 是 | tab 的列表,詳見 list 屬性說明,最少2個、最多5個 tab | ||
position | String | 否 | bottom | 可選值 bottom、top | top 值僅微信小程序支持 |
fontSize | String | 否 | 10px | 文字默認大小 | App 2.3.4+、H5 3.0.0+ |
iconWidth | String | 否 | 24px | 圖標默認寬度(高度等比例縮放) | App 2.3.4+、H5 3.0.0+ |
spacing | String | 否 | 3px | 圖標和文字的間距 | App 2.3.4+、H5 3.0.0+ |
height | String | 否 | 50px | tabBar 默認高度 | App 2.3.4+、H5 3.0.0+ |
midButton | Object | 否 | 中間按鈕 僅在 list 項為偶數時有效 | App 2.3.4+、H5 3.0.0+ |
其中 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 時,此參數無效 |
midButton 屬性說明
屬性 | 類型 | 必填 | 默認值 | 描述 |
---|---|---|---|---|
width | String | 否 | 80px | 中間按鈕的寬度,tabBar 其它項為減去此寬度后平分,默認值為與其它項平分寬度 |
height | String | 否 | 50px | 中間按鈕的高度,可以大於 tabBar 高度,達到中間凸起的效果 |
text | String | 否 | 中間按鈕的文字 | |
iconPath | String | 否 | 中間按鈕的圖片路徑 | |
iconWidth | String | 否 | 24px | 圖片寬度(高度等比例縮放) |
backgroundImage | String | 否 | 中間按鈕的背景圖片路徑 |
- 中間帶+號的tabbar模板例子,參考。可跨端,但+號不凸起。如需中間凸起,配置tabbar的midButton。