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

相关API

uni.getSystemInfoSync()
uni.getMenuButtonBoundingClientRect()

创建一个utils文件夹,该文件下封装一个systemInfo.js
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组件,在当前组件导入使用封装的,固定在顶部
html 复制代码
<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"
展示效果图
相关推荐
咸虾米_12 天前
使用uniCloud阿里云服务空间的天塌了,云函数计费规则调整了
阿里云·云计算·uniapp·unicloud
蜡台13 天前
SSE WebSocket Socket.IO 三者使用及区别
websocket·网络协议·uniapp·sse·socket.io·eventsource
路光.22 天前
uniappVue2升级Vue3内存溢出解决方式
vue·vue3·uniapp
小马_xiaoen1 个月前
常规优化已用尽?小程序体积深层次优化实战!!!
前端·小程序·uniapp
CherishXt1 个月前
修复IM完整版接入小程序后,聊天页面键盘弹起时,页面高度错误问题
vue.js·小程序·uniapp·im
Rysxt_1 个月前
Uniapp全局配置教程
前端·uniapp
嗯嗯**1 个月前
HBuilder学习1:概述、网站快速免费打包成apk
uniapp·apk·hbuilder·url快速打包成apk·网站快速打包成apk
CherishXt2 个月前
对接腾讯IM,实现个人业务系统页面按钮直接跳转到和用户的聊天页面(不需要加好友)
uniapp·即时通讯·im
巴巴博一2 个月前
UniApp实战:如何优雅地把 uv-ui (uv-qrcode) 生成的二维码保存到手机相册
微信小程序·uniapp·uvui
getaxiosluo2 个月前
uniapp开发公众号,微信设置字体大小后,禁止改变页面字体大小
vue·uniapp·微信公众平台