uniapp配置tabbar底部导航

文章发布于 2023-07-28

配置tabbar底部导航时,要注意下面两点:

  • tabbar 个数:最少为2个,不超过5个。
  • 在pages.json里配置,和pages同级。

tabbar配置 示例

pages.json
{
    "pages":[ //tabbar list项的pagePath,必须在pages里配置路由。
        {
            "path": "pages/component/index",
            "style": { 
                "navigationBarTitleText": "首页", //设置页面标题文字
            }
        }, {
            "path": "pages/api/login",
            "style": {
                "navigationBarTitleText": "首页", //设置页面标题文字
            }
        }
    ],
    "tabBar": {
        "color": "#7A7E83", //tab 上的文字默认颜色
        "selectedColor": "#3cc51f", //tab 上的文字选中时的颜色
        "borderStyle": "black",	//tabbar 上边框的颜色,可选值 black/white,也支持其他颜色值	
        "backgroundColor": "#ffffff", //tab 的背景色
        "list": [{ //tab 的列表,详见 list 属性说明,最少2个、最多5个 tab	
            "pagePath": "pages/component/index", //页面路径,必须在 pages 中先定义
            "iconPath": "static/image/icon_component.png",//图片路径,icon 大小限制为40kb,建议尺寸为 81px * 81px,当 position 为 top 时,此参数无效,不支持网络图片,不支持字体图标
            "selectedIconPath": "static/image/icon_component_HL.png",//选中时的图片路径,icon 大小限制为40kb,建议尺寸为 81px * 81px ,当 position 为 top 时,此参数无效
            "text": "组件",//tab 上按钮文字,在 App 和 H5 平台为非必填。例如中间可放一个没有文字的+号图标	
        }, {
            "pagePath": "pages/api/index",
            "iconPath": "static/image/icon_API.png",
            "selectedIconPath": "static/image/icon_API_HL.png",
            "text": "接口"
        }]
    }
}

更多详细配置文档:

属性 类型 必填 默认值 描述 平台差异说明
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 时,此参数无效
visible Boolean 该项是否显示,默认显示 App (3.2.10+)、H5 (3.2.10)+
iconfont Object 字体图标,优先级高于 iconPath App(3.4.4+)

tabbar优缺点

效率高。原生引擎在启动时无需等待js引擎初始化,即可直接读取 pages.json 中配置的 tabBar 信息,渲染原生tab。

不够灵活。无法根据角色权限动态渲染tabbar list列表项。这种情况需要使用自定义tabbar。

自定义tabbar

请看 uniapp 使用自定义底部导航来动态渲染tabbar导航

tabbar 事件

用于在实际项目中设置tabbar,比如有更新内容,abbar item栏显示一个小红点的信号,表示有新的内容未读。

文档:

api 说明
uni.setTabBarItem(OBJECT) 动态设置 tabBar 某一项的内容
uni.setTabBarStyle(OBJECT) 动态设置 tabBar 的整体样式
uni.hideTabBar(OBJECT) 隐藏 tabBar
uni.showTabBar(OBJECT) 显示 tabBar
uni.setTabBarBadge(OBJECT) 为 tabBar 某一项的右上角添加文本。
uni.removeTabBarBadge(OBJECT) 移除 tabBar 某一项右上角的文本。
uni.showTabBarRedDot(OBJECT) 显示 tabBar 某一项的右上角的红点。
uni.hideTabBarRedDot(OBJECT) 隐藏 tabBar 某一项的右上角的红点。
uni.onTabBarMidButtonTap(CALLBACK) 监听中间按钮的点击事件