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);
相关推荐
每天都要加油呀!6 小时前
TypeError: uni.requestPayment is not a function
小程序
java1234_小锋6 小时前
分享一套优质的微信小程序校园志愿者系统(SpringBoot后端+Vue3管理端)
微信小程序·小程序·校园志愿者
2501_916008897 小时前
深入解析iOS机审4.3原理与混淆实战方法
android·java·开发语言·ios·小程序·uni-app·iphone
打破砂锅问到底0077 小时前
AI 驱动开发实战:10分钟从零构建「微信群相册」小程序
人工智能·微信·小程序·ai编程
CHU7290358 小时前
扭蛋机盲盒小程序前端功能设计解析:打造趣味与惊喜并存的消费体验
前端·小程序
QT.qtqtqtqtqt8 小时前
uni-app小程序前端开发笔记(更新中)
前端·笔记·小程序·uni-app
CHU72903510 小时前
直播商城APP前端功能全景解析:打造沉浸式互动购物新体验
java·前端·小程序
喵喵虫18 小时前
uniapp修改封装组件失败 styleIsolation
uni-app
黑客老李20 小时前
web渗透实战 | js.map文件泄露导致的通杀漏洞
安全·web安全·小程序·黑客入门·渗透测试实战
游戏开发爱好者81 天前
日常开发与测试的 App 测试方法、查看设备状态、实时日志、应用数据
android·ios·小程序·https·uni-app·iphone·webview