title: swan.setTabBarStyle header: develop nav: api sidebar: tabbar_swan-setTabBarStyle
webUrl: https://qft12m.smartapps.cn/swan-api/tab-bar/tab-bar
解释:动态设置 tabBar 的整体样式
方法参数
Object object
object参数说明
| 属性名 | 类型 | 必填 | 默认值 | 说明 |
|---|---|---|---|---|
| color | HexColor | 否 | tab 上的文字默认颜色 | |
| selectedColor | HexColor | 否 | tab 上的文字选中时的颜色 | |
| backgroundColor | HexColor | 否 | tab 的背景色 | |
| borderStyle | String | 否 | tabbar上边框的颜色, 有效值 black/white | |
| success | Function | 否 | 接口调用成功的回调函数 | |
| fail | Function | 否 | 接口调用失败的回调函数 | |
| complete | Function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
示例
扫码体验
请使用百度APP扫码
代码示例1 动态设置
:::codeTab
Page({onTabItemTap(item) {console.log(item.index);if(item.index == 0){setTabBarStyle('#FFFFBD')}else if(item.index == 1){setTabBarStyle('#FFFFBE')}else {setTabBarStyle('#FFFFBF')}}setTabBarStyle(bg) {swan.setTabBarStyle({color: '#000',// blackselectedColor: '#FF0000',// redbackgroundColor: bg,borderStyle: 'black',success: function () {console.log('setTabBarStyle success');},fail: function (err) {console.log('setTabBarStyle fail', err);}});}});
:::
代码示例2 borderStyle为black
:::codeTab
Page({setTabBarStyle() {swan.setTabBarStyle({color: '#000',// blackselectedColor: '#FF0000',// redbackgroundColor: '#FFFFBD',borderStyle: 'black',success: function () {console.log('setTabBarStyle success');},fail: function (err) {console.log('setTabBarStyle fail', err);}});}});
:::
代码示例3 borderStyle为white
:::codeTab
<view class="wrap"><button type="primary" bindtap="setTabBarStyle">setTabBarStyle</button></view>
Page({setTabBarStyle() {swan.setTabBarStyle({color: '#000',// blackselectedColor: '#FF0000',// redbackgroundColor: '#FFFFBD',borderStyle: 'white',success: function () {console.log('setTabBarStyle success');},fail: function (err) {console.log('setTabBarStyle fail', err);}});}});
:::
代码示例4 tab的默认样式可在app.json中设置
:::codeTab
<view class="wrap"><button type="primary" bindtap="setTabBarStyle">setTabBarStyle</button></view>
"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 |
