当前位置: 首页 > news >正文

Uniapp 自定义头部导航栏

Uniapp 自定义头部导航栏指南

在 Uniapp 中自定义头部导航栏可以通过以下几种方式实现:

1. 全局配置

pages.json 中进行全局配置:

{"globalStyle": {"navigationBarTextStyle": "black","navigationBarTitleText": "默认标题","navigationBarBackgroundColor": "#F8F8F8","backgroundColor": "#F8F8F8"}
}
"style": {"navigationBarTitleText": "首页",      // 设置导航栏标题文字"navigationBarBackgroundColor": "#FF0000",  // 设置导航栏背景颜色"navigationBarTextStyle": "white"     // 设置导航栏文字颜色
}

2. 单页面配置

pages.json 中为特定页面配置:

{"pages": [{"path": "pages/index/index","style": {"navigationBarTitleText": "首页","navigationBarBackgroundColor": "#FF0000","navigationBarTextStyle": "white"}}]
}

3. 完全自定义导航栏

如果需要更复杂的自定义,可以隐藏原生导航栏并使用自定义组件:

  1. 首先在 pages.json 中隐藏原生导航栏:
{"pages": [{"path": "pages/index/index","style": {"navigationStyle": "custom"}}]
}
  1. 然后创建自定义导航栏组件,例如 custom-nav-bar.vue
<template><view class="custom-nav-bar"><view class="nav-bar-content"><!-- 返回按钮 --><view class="back-btn" @click="handleBack" v-if="showBack"><image src="/static/back.png" mode="aspectFit"></image></view><!-- 标题 --><view class="title">{{ title }}</view><!-- 右侧操作 --><view class="right-actions"><slot name="right"></slot></view></view></view>
</template><script>
export default {props: {title: {type: String,default: ''},showBack: {type: Boolean,default: true}},methods: {handleBack() {uni.navigateBack()}}
}
</script><style scoped>
.custom-nav-bar {height: var(--status-bar-height);padding-top: var(--status-bar-height);background-color: #FFFFFF;box-shadow: 0 1px 6px rgba(0, 0, 0, 0.1);position: fixed;top: 0;left: 0;right: 0;z-index: 999;
}.nav-bar-content {height: 44px;display: flex;align-items: center;padding: 0 15px;
}.back-btn {width: 24px;height: 24px;margin-right: 10px;
}.back-btn image {width: 100%;height: 100%;
}.title {flex: 1;text-align: center;font-size: 16px;font-weight: bold;
}.right-actions {width: 24px;height: 24px;margin-left: 10px;
}
</style>
  1. 在页面中使用自定义导航栏:
<template><view><custom-nav-bar title="我的页面" :show-back="true"><template #right><image src="/static/share.png" mode="aspectFit"></image></template></custom-nav-bar><!-- 页面内容需要设置padding-top避免被导航栏遮挡 --><view class="content" :style="{paddingTop: navBarHeight}">页面内容...</view></view>
</template><script>
import CustomNavBar from '@/components/custom-nav-bar.vue'export default {components: {CustomNavBar},data() {return {// 计算导航栏高度(状态栏高度 + 导航栏内容高度)navBarHeight: `calc(var(--status-bar-height) + 44px)`}}
}
</script>

注意事项

  1. --status-bar-height 是 CSS 变量,表示状态栏高度
  2. 导航栏内容高度通常为 44px(iOS 标准)
  3. 使用自定义导航栏时,页面内容需要设置适当的 padding-top 避免被遮挡
  4. 在微信小程序中,自定义导航栏可能需要特殊处理,可以使用 uni.getSystemInfoSync() 获取状态栏高度

平台差异

  • H5:可以直接使用 CSS 实现复杂效果
  • 小程序:部分样式可能需要特殊处理,特别是状态栏高度
  • App:支持最全面的自定义能力

通过以上方法,你可以灵活地实现各种自定义头部导航栏效果。

http://www.dtcms.com/a/330881.html

相关文章:

  • 表达式树实战:Unity动态逻辑编程
  • 考研408《计算机组成原理》复习笔记,第三章(6)——Cache(超级重点!!!)
  • 【科研绘图系列】R语言绘制蝶形条形图蝶形柱状堆积图
  • 考研408《计算机组成原理》复习笔记,第四章(3)——指令集、汇编语言
  • 一致性哈希Consistent Hashing
  • Rust Web框架Axum学习指南之入门初体验
  • Java面试宝典:JVM性能优化
  • 【代码随想录day 20】 力扣 669. 修剪二叉搜索树
  • MySQL 性能优化实战指南:释放数据库潜能的艺术
  • 【visual studio】visual studio配置环境opencv和onnxruntime
  • 零知开源——基于STM32F4的HC-12无线通信系统及ST7789显示应用
  • 【Linux】库制作与原理
  • mysql卸载了 服务内还显示如何解决
  • CVPR 2025丨时间序列:动态多尺度机制登场,即插即用,预测稳准狠刷新SOTA
  • Vivado GPIO详解
  • 量化因子RSI
  • 中小型泵站物联网智能控制系统解决方案:构建无人值守的自动化泵站体系
  • 基于STM32单片机智能手表GSM短信上报GPS定位温湿度检测记步设计
  • OS设备UDID查看方法
  • Mybatis学习笔记(一)
  • 「iOS」————设计架构
  • 在语音通信业务量下降时候该怎么做
  • PHP现代化全栈开发:微服务架构与云原生实践
  • 基于FPGA的8PSK+卷积编码Viterbi译码通信系统,包含帧同步,信道,误码统计,可设置SNR
  • 一台联想 ThinkCentre M7100z一体机开机黑屏无显示维修记录
  • 优化 SQL 查询:选出每个分组最大值的行并插入 10W 条测试数据
  • 云计算-Docker Compose 实战:从OwnCloud、WordPress、SkyWalking、Redis ,Rabbitmq等服务配置实例轻松搞定
  • 【代码随想录day 20】 力扣 108.将有序数组转换为二叉搜索树
  • 老生常谈之引用计数:《More Effective C++》条款29
  • 炎热的夏天