Uniapp 自定义头部导航栏

Uniapp 自定义头部导航栏指南

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

1. 全局配置

pages.json 中进行全局配置:

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

2. 单页面配置

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

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

3. 完全自定义导航栏

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

  1. 首先在 pages.json 中隐藏原生导航栏:
json 复制代码
{
  "pages": [
    {
      "path": "pages/index/index",
      "style": {
        "navigationStyle": "custom"
      }
    }
  ]
}
  1. 然后创建自定义导航栏组件,例如 custom-nav-bar.vue
html 复制代码
<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. 在页面中使用自定义导航栏:
html 复制代码
<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:支持最全面的自定义能力

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

相关推荐
梦帮科技3 小时前
GRAVIS v4.0:基于Web的极速套利架构设计与实时数据流实现
前端·人工智能·rust·自动化·区块链·智能合约·数字货币
爱勇宝4 小时前
办公资料反复修改、补传、交接混乱,我做了个桌面工具来解决这件事
前端·后端·程序员
微信开发api-视频号协议5 小时前
企业微信外部群开发自动化实践过程
java·前端·微信·自动化·企业微信·ipad
甲维斯5 小时前
Fable5:20美金的顶级设计师!
前端·人工智能
kyriewen5 小时前
我同时用了 Claude Code、Cursor 和 Codex,说说三个工具的真实差距——2026 年选错工具比不用 AI 更浪费时间
前端·ai编程·claude
CHB5 小时前
uni-app x 蒸汽模式 iOS版 性能测试基准报告 Benchmark
ios·uni-app·swiftui
Jackson__5 小时前
为了拯救我的腰椎和颈椎,我做了款浏览器插件!
前端·javascript·开源
衍生星球5 小时前
SpringBoot3 + Vue3 + 微信小程序如何学习,以及学哪些技术,组件
sql·微信小程序·uni-app·vue·springboot
闲猫5 小时前
Python FastAPI + SQLAlchemy 入门教程:从零搭建你的第一个 Web 应用
前端·python·fastapi
林小帅6 小时前
NestJS v11 + Prisma v7 ESM 迁移配置解析
前端·javascript·后端