uni-app学习笔记二十一--pages.json中tabBar设置底部菜单项和图标
如果应用是一个多 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"}]}
效果: