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

uniapp获取状态栏高度,胶囊按钮的高度,底部安全区域的高度,自定义导航栏

相关API

uni.getSystemInfoSync()
uni.getMenuButtonBoundingClientRect()

创建一个utils文件夹,该文件下封装一个systemInfo.js
/*** 系统信息工具类* 封装获取系统状态栏、导航栏和安全区域等相关信息的方法*/// 获取系统信息并缓存
const systemInfo = uni.getSystemInfoSync();
//判断设备,H5没有胶囊按钮
// #ifndef H5
const menuButtonInfo = uni.getMenuButtonBoundingClientRect();
// #endif// 默认值常量
const DEFAULT_STATUS_BAR_HEIGHT = 10;
const DEFAULT_BAR_HEIGHT = 44;
const DEFAULT_SAFE_AREA_INSETS = { bottom: 10, top: 0 };/*** 获取状态栏高度* @returns {string|number} 状态栏高度,默认返回10px*/
export const getStatusBarHeight = () => {return systemInfo.statusBarHeight || DEFAULT_STATUS_BAR_HEIGHT;
};/*** 获取导航栏总高度(包括状态栏和标题栏)* @returns {number} 导航栏总高度,默认返回44*/
export const getNavigationBarHeight = () => {try {const statusBarHeight = Number(systemInfo.statusBarHeight) || 0;return (menuButtonInfo.top - statusBarHeight) * 2 + menuButtonInfo.height;} catch (error) {return DEFAULT_BAR_HEIGHT;}
};/*** 获取菜单按钮上边距* @returns {number} 菜单按钮上边距*/
export const getMenuButtonTop = () => {return menuButtonInfo.top;
};/*** 获取安全区域信息* @returns {Object} 包含bottom和top属性的安全区域对象*/
export const getSafeAreaInsets = () => {return {bottom: systemInfo.safeAreaInsets?.bottom || DEFAULT_SAFE_AREA_INSETS.bottom,top: systemInfo.safeAreaInsets?.top || DEFAULT_SAFE_AREA_INSETS.top};
};export default {getStatusBarHeight,getNavigationBarHeight,getMenuButtonTop,getSafeAreaInsets
};

在这里插入图片描述

封装一个nav-header.vue组件,在当前组件导入使用封装的,固定在顶部
<template><view class="nav-layout"><!-- 状态栏高度 --><view class="status-bar" :style="{ height: `${getStatusBarHeight()}px` }"></view><!-- 自定义导航栏 --><view class="search-bar" :style="{ height: `${getNavigationBarHeight()}px` }"><text class="recommend">推荐</text><view class="search-container" @click="onSearchClick"><uni-icons class="search-icon" type="search" size="22" color="#333" /><text class="search-placeholder">搜索</text></view></view></view><!-- 占位 --><view class="layout-fill" :style="{ height: `${getStatusBarHeight() + getNavigationBarHeight()}px` }"></view>
</template><script setup>
import {getStatusBarHeight,getNavigationBarHeight} from '@/utils/systemInfo.js'
</script><style lang="scss" scoped>
.nav-layout {position: fixed;top: 0;left: 0;z-index: 10;width: 100%;background: linear-gradient(to bottom, transparent, #fff 400rpx),linear-gradient(to right, #beecd8 20%, #f4e2d8);.status-bar {background-color: transparent;}.search-bar {display: flex;align-items: center;padding: 0 30rpx;.recommend {font-size: 40rpx;font-weight: bold;padding-right: 30rpx;color: #333;}}.search-container {display: flex;align-items: center;border-radius: 40rpx;border: 2px solid #fff;width: 300rpx;padding: 0 20rpx;box-sizing: border-box;background: rgba(255, 255, 255, 0.3);transition: all 0.3s ease;&:hover {background: rgba(255, 255, 255, 0.5);transform: scale(1.02);}.search-icon {padding-right: 6rpx;}.search-placeholder {color: #666;font-size: 28rpx;}}
}.layout-fill {width: 100%;
}
</style>
需要使用的页面导入nav-header.vue组件,自定义导航栏首先需要在page.json文件对应的路径去配置"navigationStyle": "custom"

在这里插入图片描述

展示效果图

在这里插入图片描述

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

相关文章:

  • [实战]调频三角波和锯齿波信号生成(完整C代码)
  • hbuilderx打包的应用上传苹果应用商店最简方法
  • 字节豆包又一个新功能,超级实用,4 种玩法,你肯定用得上!(建议收藏)
  • Uniapp视频聊天软件内容监控插件开发指南
  • OA系统中的搜索功能方案:简单搜索vs高级搜索
  • 2-Git提交本地项目到远程仓库
  • 问有几条病狗?
  • 【linux网络】深入理解 TCP/UDP:从基础端口号到可靠传输机制全解析
  • 机器学习-06(Optimization-自动调整学习率)
  • consul 的安装与服务发现
  • MOSS-TTSD V2版 - 文本到语音对话生成 支持零样本多人语音克隆 一键整合包下载
  • 一文速览DeepSeek-R1的本地部署——可联网、可实现本地知识库问答(附教程)
  • OBB旋转框检测配置与训练全流程(基于 DOTA8 数据集)
  • 第3章 操作臂运动学(笔记总结)
  • Hangfire 调用报错解决方案总结
  • 经典的垃圾收集器!!!
  • day02-数组part02
  • day67—DFS—被围绕的区域(LeetCode-130)
  • 飞算JavaAI 实战笔记
  • Qt中QGraphicsView类应用解析:构建高效2D图形界面的核心技术
  • 迭代器(c++)、智能指针
  • 【C/C++】动态内存分配:从 C++98 裸指针到现代策略
  • PyTorch武侠演义 第一卷:初入江湖 第1章:武林新秀遇Tensor - 张量基础
  • 技术突破与落地应用:端到端 2.0 时代辅助驾驶TOP10 论文深度拆解系列【第九篇(排名不分先后)】
  • 飞书CEO谢欣:挑战巨头,打造AI新时代的Office
  • Rail开发日志_6
  • Python类型注解中的`Optional`:深入理解难点解析(进阶版)
  • EndNote快速入手指南
  • CDN 加速与安全防护:双剑合璧的技术协同
  • manifest.json只有源码视图没其他配置