uniapp小程序顶部状态栏占位和自定义头部导航栏

子组件custom-header.vue

js 复制代码
<template>
  <view class="custom-header">
    <!-- 顶部状态栏占位 -->
    <view class="status-bar" :style="{ height: statusBarHeight + 'px' }"></view>

    <!-- 导航栏 -->
    <view class="nav-bar" :style="navBarStyle">
      <!-- 左侧插槽内容 -->
      <view class="left-slot">
        <text style="line-height: 44px;">返回</text>
      </view>

      <!-- 中间插槽内容 -->
      <view class="center-slot">
        <text style="line-height: 44px;">标题</text>
      </view>

      <!-- 右侧插槽内容 -->
      <view class="right-slot">
        <text style="line-height: 44px;">更多</text>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    name: 'CustomHeader',
    data() {
      return {
        statusBarHeight: 0, // 状态栏高度
        navBarHeight: '44px', // 导航栏高度
        paddingTop: '0px', // 导航栏上边距
      };
    },
    computed: {
      navBarStyle() {
        return {
          height: this.navBarHeight,
          paddingTop: this.paddingTop,
        };
      },
    },
    mounted() {
      this.getSystemInfo();
      this.getMenuButtonInfo();
    },
    methods: {
      // 获取系统信息
      getSystemInfo() {
        const systemInfo = uni.getSystemInfoSync();
        this.statusBarHeight = systemInfo.statusBarHeight || 0;
        console.log('状态栏高度:', this.statusBarHeight);
      },
      // 获取胶囊按钮信息
      getMenuButtonInfo() {
        if (uni.getMenuButtonBoundingClientRect) {
          const menuButtonInfo = uni.getMenuButtonBoundingClientRect();
          console.log('胶囊按钮信息:', menuButtonInfo);

          const paddingTop = menuButtonInfo.top - this.statusBarHeight;
          const navBarHeight = menuButtonInfo.height + paddingTop * 2;

          // 确保值是字符串,并包含单位
          this.navBarHeight = `${navBarHeight}px`;
          this.paddingTop = `${paddingTop}px`;
        } else {
          console.warn('当前环境不支持获取胶囊按钮信息');
        }
      },
    },
  };
</script>

<style scoped>
  .custom-header {
    width: 100%;
  }

  .status-bar {
    background-color: transparent;
  }

  .nav-bar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-left: 10px;
    padding-right: 10px;
    background-color: #ffffff;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  }

  .left-slot,
  .center-slot,
  .right-slot {
    display: flex;
    align-items: center;
    height: 100%; // 确保插槽内容高度撑满导航栏
  }

  .center-slot {
    flex: 1;
    justify-content: center;
  }

  .right-slot {
    justify-content: flex-end;
  }
</style>

父组件 index.vue

html 复制代码
<template>
<!-- 使用自定义头部组件 -->
		<custom-header>
			<!-- 左侧插槽内容 -->
			<template v-slot:left>
				<text>返回</text>
			</template>

			<!-- 中间插槽内容 -->
			<template v-slot:center>
				<text>标题</text>
			</template>

			<!-- 右侧插槽内容 -->
			<template v-slot:right>
				<text>更多</text>
			</template>
		</custom-header>
</template>

全局注册 main.js

js 复制代码
// 引入自定义头部组件
import CustomHeader from '@/components/custom-header.vue';

// 全局注册组件
Vue.component('CustomHeader', CustomHeader);
相关推荐
Mintopia5 小时前
Vue3 项目如何迁移到 uni-app x:从纯 Web 到多端应用的系统指南
uni-app
Mintopia6 小时前
uni-app x 发展前景技术分析:跨端统一的新阶段?
uni-app
不爱说话郭德纲1 天前
告别漫长的HbuilderX云打包排队!uni-app x 安卓本地打包保姆级教程(附白屏、包体积过大排坑指南)
android·前端·uni-app
HashTang2 天前
【AI 编程实战】第 12 篇:从 0 到 1 的回顾 - 项目总结与 AI 协作心得
前端·uni-app·ai编程
JunjunZ2 天前
uniapp 文件预览:从文件流到多格式预览的完整实现
前端·uni-app
郑州光合科技余经理3 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
TT_Close3 天前
“啪啪啪”三下键盘,极速拉起你的 uni-app 项目!
vue.js·uni-app·前端工程化
特立独行的猫a3 天前
uni-app x跨平台开发实战:开发鸿蒙HarmonyOS影视票房榜组件完整实现过程
华为·uni-app·harmonyos·轮播图·uniapp-x
吴声子夜歌3 天前
小程序——布局示例
小程序
luffy54593 天前
微信小程序页面使用类似filter函数的wxs语法
微信小程序·小程序