uni-app 之 设置 tabBar

tabBar 是移动应用中常见的导航模式,uni-app 提供了丰富的 API 来动态控制 tabBar 的外观和行为。

1. uni.setTabBarItem(object)

动态设置 tabBar 某一项的内容

参数说明
属性 类型 默认值 必填 说明
index number tabBar 的哪一项,从左边算起
text string tab 上的按钮文字
iconPath string 图片路径,icon 大小限制为 40kb
selectedIconPath string 选中时的图片路径,icon 大小限制为 40kb
success function 接口调用成功的回调函数
fail function 接口调用失败的回调函数
complete function 接口调用结束的回调函数
示例代码
javascript 复制代码
uni.setTabBarItem({
  index: 0,
  text: "首页",
  iconPath: "/static/icon/home.png",
  selectedIconPath: "/static/icon/home-active.png",
});

2. uni.setTabBarStyle(object)

动态设置 tabBar 的整体样式

参数说明
属性 类型 默认值 必填 说明
color string tab 上的文字默认颜色
selectedColor string tab 上的文字选中时的颜色
backgroundColor string tab 的背景色
borderStyle string tabBar 上边框的颜色,仅支持 black/white
success function 接口调用成功的回调函数
fail function 接口调用失败的回调函数
complete function 接口调用结束的回调函数
示例代码
javascript 复制代码
uni.setTabBarStyle({
  color: "#7A7E83",
  selectedColor: "#007AFF",
  backgroundColor: "#F8F8F8",
  borderStyle: "black",
});

3. uni.hideTabBar(object)

隐藏 tabBar

参数说明
属性 类型 默认值 必填 说明
animation boolean false 是否需要动画效果
success function 接口调用成功的回调函数
fail function 接口调用失败的回调函数
complete function 接口调用结束的回调函数
示例代码
javascript 复制代码
// 隐藏 tabBar,带动画效果
uni.hideTabBar({
  animation: true,
});

4. uni.showTabBar(object)

显示 tabBar

参数说明
属性 类型 默认值 必填 说明
animation boolean false 是否需要动画效果
success function 接口调用成功的回调函数
fail function 接口调用失败的回调函数
complete function 接口调用结束的回调函数
示例代码
javascript 复制代码
// 显示 tabBar,带动画效果
uni.showTabBar({
  animation: true,
});

5. uni.setTabBarBadge(object)

为 tabBar 某一项右上角添加文本

参数说明
属性 类型 默认值 必填 说明
index number tabBar 的哪一项,从左边算起
text string 显示的文本,超过 4 个字符则显示为 ...
success function 接口调用成功的回调函数
fail function 接口调用失败的回调函数
complete function 接口调用结束的回调函数
示例代码
javascript 复制代码
// 为第一个 tab 添加 badge
uni.setTabBarBadge({
  index: 0,
  text: "3",
});

6. uni.removeTabBarBadge(object)

移除 tabBar 某一项右上角的文本

参数说明
属性 类型 默认值 必填 说明
index number tabBar 的哪一项,从左边算起
success function 接口调用成功的回调函数
fail function 接口调用失败的回调函数
complete function 接口调用结束的回调函数
示例代码
javascript 复制代码
// 移除第一个 tab 的 badge
uni.removeTabBarBadge({
  index: 0,
});

7. uni.showTabBarRedDot(object)

显示 tabBar 某一项的右上角红点

参数说明
属性 类型 默认值 必填 说明
index number tabBar 的哪一项,从左边算起
success function 接口调用成功的回调函数
fail function 接口调用失败的回调函数
complete function 接口调用结束的回调函数
示例代码
javascript 复制代码
// 显示第一个 tab 的红点
uni.showTabBarRedDot({
  index: 0,
});

8. uni.hideTabBarRedDot(object)

隐藏 tabBar 某一项的右上角红点

参数说明
属性 类型 默认值 必填 说明
index number tabBar 的哪一项,从左边算起
success function 接口调用成功的回调函数
fail function 接口调用失败的回调函数
complete function 接口调用结束的回调函数
示例代码
javascript 复制代码
// 隐藏第一个 tab 的红点
uni.hideTabBarRedDot({
  index: 0,
});

完整示例

javascript 复制代码
export default {
  methods: {
    // 初始化 tabBar 样式
    initTabBar() {
      // 设置 tabBar 整体样式
      uni.setTabBarStyle({
        color: "#7A7E83",
        selectedColor: "#007AFF",
        backgroundColor: "#FFFFFF",
        borderStyle: "black",
      });

      // 设置各 tab 项
      uni.setTabBarItem({
        index: 0,
        text: "首页",
        iconPath: "/static/tabbar/home.png",
        selectedIconPath: "/static/tabbar/home-selected.png",
      });

      uni.setTabBarItem({
        index: 1,
        text: "分类",
        iconPath: "/static/tabbar/category.png",
        selectedIconPath: "/static/tabbar/category-selected.png",
      });

      uni.setTabBarItem({
        index: 2,
        text: "购物车",
        iconPath: "/static/tabbar/cart.png",
        selectedIconPath: "/static/tabbar/cart-selected.png",
      });

      uni.setTabBarItem({
        index: 3,
        text: "我的",
        iconPath: "/static/tabbar/user.png",
        selectedIconPath: "/static/tabbar/user-selected.png",
      });
    },

    // 更新购物车数量显示
    updateCartBadge(count) {
      if (count > 0) {
        uni.setTabBarBadge({
          index: 2,
          text: count.toString(),
        });
      } else {
        uni.removeTabBarBadge({
          index: 2,
        });
      }
    },

    // 显示消息红点
    showMessageDot(show) {
      if (show) {
        uni.showTabBarRedDot({
          index: 3,
        });
      } else {
        uni.hideTabBarRedDot({
          index: 3,
        });
      }
    },

    // 在特定页面隐藏 tabBar
    hideTabBarInPage() {
      uni.hideTabBar({
        animation: true,
      });
    },

    // 返回页面时显示 tabBar
    showTabBarInPage() {
      uni.showTabBar({
        animation: true,
      });
    },
  },

  mounted() {
    this.initTabBar();
  },
};

注意事项

  1. 所有 tabBar 相关 API 都需要在 tabBar 已经初始化完成后再调用
  2. iconPathselectedIconPath 必须使用本地资源路径
  3. badge 文本超过 4 个字符会显示为 ...
  4. 红点和 badge 不能同时显示在同一项上
  5. 在不同平台上的表现可能会有细微差异
  6. 修改 tabBar 样式时建议在应用启动时进行,避免频繁修改影响性能
相关推荐
Gofarlic_oms14 小时前
利用API实现ANSYS许可证管理自动化集成
运维·服务器·开发语言·matlab·自动化·负载均衡
档案宝档案管理5 小时前
权限分级管控,全程可追溯,筑牢会计档案安全防线
运维·网络·人工智能
倔强的石头1066 小时前
【Linux指南】基础IO系列(八):实战衔接 —— 给微型 Shell 添加完整重定向功能
linux·运维·服务器
郑州光合科技余经理7 小时前
同城O2O海外版二次开发实战:从支付网关到配送算法
开发语言·前端·后端·算法·架构·uni-app·php
观北海7 小时前
AiScan-N:AI全自动化渗透测试工具的深度技术解析
运维·自动化
Ujimatsu7 小时前
虚拟机安装Ubuntu 26.04.x及其常用软件(2026.4)
linux·运维·ubuntu
冰暮流星7 小时前
javascript事件案例-全选框案例
服务器·前端·javascript
Agent产品评测局10 小时前
制造业生产调度自动化落地,完整步骤与避坑指南:2026企业级智能体选型与实战全景
运维·人工智能·ai·chatgpt·自动化
狂奔的sherry10 小时前
一次由 mount 引发的 Linux 文件系统“错觉”
linux·运维·服务器
志栋智能10 小时前
超自动化巡检:让合规与审计变得轻松简单
运维·网络·人工智能·自动化