docs/api/set-navigation-bar-color.md
::: sourceCode
GitCode: https://gitcode.com/dcloud/uni-api/tree/alpha/uni_modules/uni-navigationBar
GitHub: https://github.com/dcloudio/uni-api/tree/alpha/uni_modules/uni-navigationBar
:::
设置导航条、状态栏颜色
| Web | 微信小程序 | Android | iOS | HarmonyOS | HarmonyOS(Vapor) |
|---|---|---|---|---|---|
| 4.0 | 4.41 | √ | 4.11 | 4.61 | 5.0 |
注意当pages.json中设置导航栏为custom时:
| 名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
|---|---|---|---|---|---|
| options | SetNavigationBarColorOptions | 是 | - | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - |
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
|---|---|---|---|---|---|
| frontColor | string | 是 | - | Web: 4.0; 微信小程序: 4.41; Android: √; iOS: 4.11; HarmonyOS: 4.61; HarmonyOS(Vapor): 5.0 | 前景颜色值,包括按钮、标题、状态栏的颜色,仅支持 #ffffff 和 #000000 |
| backgroundColor | string.ColorString | 是 | - | Web: 4.0; 微信小程序: 4.41; Android: √; iOS: 4.11; HarmonyOS: 4.61; HarmonyOS(Vapor): 5.0 | 背景颜色值,有效值为十六进制颜色 |
| success | (result: SetNavigationBarColorSuccess) => void | 否 | - | Web: 4.0; 微信小程序: 4.41; Android: √; iOS: 4.11; HarmonyOS: 4.61; HarmonyOS(Vapor): 5.0 | 接口调用成功的回调函数 |
| fail | (error: SetNavigationBarColorFail) => void | 否 | - | Web: 4.0; 微信小程序: 4.41; Android: √; iOS: 4.11; HarmonyOS: 4.61; HarmonyOS(Vapor): 5.0 | 接口调用失败的回调函数 |
| complete | (res: SetNavigationBarColorComplete) => void | 否 | - | Web: 4.0; 微信小程序: 4.41; Android: √; iOS: 4.11; HarmonyOS: 4.61; HarmonyOS(Vapor): 5.0 | 接口调用结束的回调函数(调用成功、失败都会执行) |
| animation | SetNavigationBarColorOptionsAnimation | 否 | - | Web: -; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: - | 动画效果 |
| 合法值 | 兼容性 | 描述 |
|---|---|---|
| #ffffff | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - | - |
| #000000 | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - | - |
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
|---|---|---|---|---|---|
| duration | number | 否 | - | Web: -; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: - | 动画变化时间,单位 ms |
| timingFunc | string | 否 | - | Web: -; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: - | 动画变化方式 |
可选值:
| 合法值 | 兼容性 | 描述 |
|---|---|---|
| linear | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - | - |
| easeIn | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - | - |
| easeOut | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - | - |
| easeInOut | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - | - |
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
|---|---|---|---|---|---|
| errMsg | string | 是 | - | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - |
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
|---|---|---|---|---|---|
| errCode | number | 是 | - | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - | 设置导航栏字体颜色错误码 |
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
|---|---|---|---|---|---|
| errMsg | string | 是 | - | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - |
| 类型 | 必备 |
|---|---|
| Promise<SetNavigationBarColorSuccess> | 否 |
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
|---|---|---|---|---|---|
| errMsg | string | 是 | - | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - |
示例为hello uni-app x alpha分支,与最新HBuilderX Alpha版同步。与最新正式版同步的master分支示例另见 ::: preview https://hellouniappx.dcloud.net.cn/web/#/pages/API/set-navigation-bar-color/set-navigation-bar-color
示例
<template>
<page-head title="setNavigationBarColor"></page-head>
<view class="uni-padding-wrap uni-common-mt">
<button @tap="setNavigationBarColor1" class="uni-btn">
设置导航条背景绿色,标题白色
</button>
<button @tap="setNavigationBarColor2" class="uni-btn">
设置导航条背景红色,标题黑色
</button>
<button @tap="goNavbarLite" class="uni-btn">
跳转自定义导航栏页面
</button>
</view>
</template>
<script setup lang="uts">
import { state, setLifeCycleNum } from '@/store/index.uts'
// 自动化测试
const getLifeCycleNum = () : number => {
return state.lifeCycleNum
}
// 自动化测试
const setLifeCycleNumFunc = (num : number) => {
setLifeCycleNum(num)
}
const setNavigationBarColor1 = () => {
uni.setNavigationBarColor({
frontColor: '#ffffff',
backgroundColor: '#00ff00',
success: () => {
console.log('setNavigationBarColor success')
setLifeCycleNumFunc(state.lifeCycleNum + 1)
},
fail: () => {
console.log('setNavigationBarColor fail')
setLifeCycleNumFunc(state.lifeCycleNum - 1)
},
complete: () => {
console.log('setNavigationBarColor complete')
setLifeCycleNumFunc(state.lifeCycleNum + 1)
}
})
}
const setNavigationBarColor2 = () => {
uni.setNavigationBarColor({
frontColor: '#000000',
backgroundColor: '#ff0000',
success: () => {
console.log('setNavigationBarColor success')
setLifeCycleNumFunc(state.lifeCycleNum + 1)
},
fail: () => {
console.log('setNavigationBarColor fail')
setLifeCycleNumFunc(state.lifeCycleNum - 1)
},
complete: () => {
console.log('setNavigationBarColor complete')
setLifeCycleNumFunc(state.lifeCycleNum + 1)
}
})
}
const goNavbarLite = () => {
uni.navigateTo({
url: '/pages/template/navbar-lite/navbar-lite'
})
}
defineExpose({
getLifeCycleNum,
setLifeCycleNum: setLifeCycleNumFunc,
setNavigationBarColor1,
setNavigationBarColor2
})
</script>
:::
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
|---|---|---|---|---|---|
| errMsg | string | 是 | - | Web: -; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: - | 错误信息 |