Skip to content

底部菜单 ​

写在入口 deekeScript.json 的 bottomMenus,颜色写在 window.tabBar。页面里用 this.setTabBar 创建/恢复整栏,用 this.setTabBarItem / this.setTabBarStyle 改运行时状态。

右侧从首页进。点按钮改某一项的文字、角标、隐藏,或只改整栏文字色、图标色、背景。再切 Tab 看效果。完整示例在 Pro「示例」→「能力 → 底部菜单」。

先看效果 ​

配置 ​

json
{
  "window": {
    "tabBar": {
      "color": "#6F7978",
      "selectedColor": "#006A65",
      "background": "#FFFFFF",
      "borderColor": "#EEEEEE"
    }
  },
  "bottomMenus": [
    { "title": "首页", "icon": "img/home.svg", "page": "pages/home" },
    { "title": "组件", "icon": "img/apps.svg", "page": "pages/components", "badge": 3 },
    { "title": "能力", "icon": "img/setting.svg", "page": "pages/ability" }
  ]
}

不配或空数组则没有底栏。字段见 配置。页内选项卡是另一个组件,见 组件 · Tabs。

页面方法 ​

二级页默认没有底栏。this.setTabBar({ items }) 在当前页创建一栏;this.setTabBar() 写回入口 bottomMenus。用 page(文件夹或 id)或 index(从 0 起)定位某一项。

setTabBar ​

参数说明
items菜单数组,字段与入口 bottomMenus 相同。不传则恢复默认
其余与 setTabBarStyle 相同,创建时一并生效
javascript
this.setTabBar({
  items: [
    { title: '首页', icon: 'img/home.svg', page: 'pages/home' },
    { title: '组件', icon: 'img/apps.svg', page: 'pages/components', badge: 3 },
    { title: '能力', icon: 'img/setting.svg', page: 'pages/ability' }
  ]
});
this.setTabBar();

setTabBarItem ​

参数说明
page / index改哪一项
badge角标。0、'' 表示去掉
hiddentrue 隐藏这一项,false 再显示
title改文字
icon / selectedIcon改图标路径
javascript
this.setTabBarItem({ page: 'pages/components', badge: 9 });
this.setTabBarItem({ page: 'pages/components', title: '控件' });
this.setTabBarItem({ page: 'pages/components', hidden: true });

颜色是整栏一起改的,不能只改某一项的图标色。某一项的文字用 title。setTabBarStyle 只覆盖传入的字段:改文字色不会动图标色,改背景也不会把颜色改回去。

setTabBarStyle ​

参数说明
color未选中项的文字颜色。只改这一项时,图标保持原色
selectedColor选中项的文字颜色。只改这一项时,图标保持原色
iconColor未选中项的图标颜色。入口未写时初始跟 color
selectedIconColor选中项的图标颜色。入口未写时初始跟 selectedColor
background底栏背景
borderColor顶部分割线
fontSize文字大小(sp)
hiddentrue 隐藏整栏
javascript
this.setTabBarStyle({
  color: '#C2410C',
  selectedColor: '#EA580C'
});
this.setTabBarStyle({
  iconColor: '#C2410C',
  selectedIconColor: '#EA580C'
});
this.setTabBarStyle({
  background: '#1A1A1A',
  borderColor: '#333333'
});

离开时恢复 ​

改底栏是全局的。示例进入独立页时 setTabBar({ items }) 创建底栏,离开时 this.setTabBar() 写回入口配置。

javascript
Page({
  onShow() {
    this.setTabBar({
      items: [
        { title: '首页', icon: 'img/home.svg', page: 'pages/home' },
        { title: '组件', icon: 'img/apps.svg', page: 'pages/components', badge: 3 },
        { title: '能力', icon: 'img/setting.svg', page: 'pages/ability' }
      ]
    });
  },
  onHide() {
    this.setTabBar();
  },
  onUnload() {
    this.setTabBar();
  }
});

切 Tab 用 this.switchTab,见 页面跳转。