codecamp

百度智能小程序 动态设置整体样式

swan.setTabBarStyle

解释:动态设置 tabBar 的整体样式,底部标签栏位于小程序底部,方便用户在不同功能模块之间进行快速切换。为保证可用性,底部导航栏承载 2-5 个功能,如果超出 5 个功能项,请酌情移入页面或菜单内。设计文档详见 底部标签栏

方法参数

Object object

object参数说明

属性名 类型 必填 默认值 说明

color

HexColor

tab 上的文字默认颜色

selectedColor

HexColor

tab 上的文字选中时的颜色

backgroundColor

HexColor

tab 的背景色

borderStyle

String

tabbar 上边框的颜色, 有效值 black/white

success

Function

接口调用成功的回调函数

fail

Function

接口调用失败的回调函数

complete

Function

接口调用结束的回调函数(调用成功、失败都会执行)

示例



图片示例

代码示例 1 - 动态设置 

在开发者工具中打开

<view class="wrap">
    <button type="primary" bindtap="customStyle">
        {{ !hasCustomedStyle ? '自定义Tab样式' : '移除自定义样式'}}
    </button>
</view>
Page({
    data: {
        defaultTabBarStyle: {
            color: '#7A7E83',
            selectedColor: '#3cc51f',
            backgroundColor: '#ffffff'
        },
        defaultItemName: '接口'
    },
    customStyle() {
        if (this.data.hasCustomedStyle) {
            this.removeCustomStyle()
            return
        }
        this.setData({hasCustomedStyle: true})
        swan.setTabBarStyle({
            color: '#FFF',
            selectedColor: '#1AAD19',
            backgroundColor: '#000000'
        })
    },

    removeCustomStyle() {
        this.setData({hasCustomedStyle: false})
        swan.setTabBarStyle(this.data.defaultTabBarStyle)
    }
});

设计指南

标签项应明确区分默认态和选中态,方便用户定位当前所在位置;图标风格应保存一致;每个标签项的文字信息不应超出 5 个中文字符,否则将被截断。

配置背景颜色时,请注意整体页面效果、及标签项的可读性和可用性。

错误

图标的默认态和选中态无明显区别,只能通过文字颜色判断当前位置。

错误

图标与文字信息颜色不统一,背景与标签配色不协调,过多使用高饱和度颜色等,均会降低阅读的舒适度。


代码示例 2 - 设置 borderStyle 

在开发者工具中打开

Page({
    setTabBarStyle() {
        swan.setTabBarStyle({
            color: '#000', // black
            selectedColor: '#FF0000', // red
            backgroundColor: '#FFFFBD',
            borderStyle: 'black', // 可选值还有white
            success: () => {
                console.log('setTabBarStyle success');
            },
            fail: err => {
                console.log('setTabBarStyle fail', err);
            }
        });
    }
});


代码示例 3 - tab 的默认样式可在 app.json 中设置 

在开发者工具中打开

"tabBar": {
    "list": [
        {
            "pagePath": "pages/index/index",
            "text": "首页",
            "iconPath":"/images/API_normal.png",
            "selectedIconPath":"/images/API_selected.png"
    },
        {
            "pagePath": "pages/detail/detail",
            "text": "详情",
            "iconPath":"/images/component_normal.png",
            "selectedIconPath":"/images/component_selected.png"
        }
    ],
    "backgroundColor" : "#ffffff",
    "borderStyle": "white",
    "color": "#000",
    "selectedColor": "#6495ED"
}

错误码

Android

错误码说明

1001

执行失败

iOS

错误码说明

1001

当前页面不含 tabbar


百度智能小程序 动态设置内容
百度智能小程序 为右上角添加文本
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

百度智能小程序开发文档

百度智能小程序 组件

百度智能小程序 地图

百度智能小程序 画布

百度智能小程序 API

百度智能小程序 界面

百度智能小程序 关注小程序引导组件

百度智能小程序 自定义组件

百度智能小程序 媒体

百度智能小程序 设备

百度智能小程序 拨打电话

百度智能小程序 内存警报

百度智能小程序 手机联系人

百度智能小程序 用户截屏事件

百度智能小程序 第三方平台

百度智能小程序 开放接口

百度智能小程序 百度收银支付

百度智能小程序 分包预下载

百度智能小程序 数据分析

百度智能小程序 服务端

百度智能小程序 云开发

百度智能小程序 初始化

百度智能小程序 云函数

百度智能小程序 服务端初始化

百度智能小程序 服务器获取上下文

百度智能小程序 服务端云函数

百度智能小程序 开发教程

百度智能小程序 功能开发

百度智能小程序 基本原理

百度智能小程序 小程序自动化

百度智能小程序 视频教程

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }