【uniapp】小程序开发6:自定义状态栏

一、自定义状态栏

可以设置某个页面的状态栏自定义或者全局状态栏自定义。

这里以首页状态栏为例。

1)pages.json 中配置"navigationStyle": "custom",代码如下:

json 复制代码
{
	"pages": [ 
		{
			"path": "pages/index/index",
			"style": {
				"navigationStyle": "custom",
				"navigationBarTitleText": "首页"
			}
		}
	],
	
	"globalStyle": {
		// 如果要配置全局自定义状态就是下面这行代码
		// "navigationStyle": "custom"
		//...
	}
}

2)增加自定义状态栏组件CustomNavbar.vue

因每台手机的顶部预留高度不一样,需要通过方法uni.getSystemInfoSync() 获取顶部安全区域高度,然后通过样式预留出来。

下面代码通过设置样式 paddingTop: safeAreaInsets?.top + 'px'预留顶部安全高度

html 复制代码
<template>
	<view :style="{ paddingTop: safeAreaInsets?.top + 'px' }">
		<uni-nav-bar shadow :border="false" title="自定义导航栏" leftIcon="arrow-left" leftText="首页">
		</uni-nav-bar>
	</view>
	<uni-search-bar radius="5" placeholder="自动显示隐藏" clearButton="auto" cancelButton="none" @confirm="search" />
</template>

<script setup lang="ts">
const { safeAreaInsets } = uni.getSystemInfoSync()
console.log('safeAreaInsets', safeAreaInsets);

const search = (res: any) => {
	uni.showToast({
		title: '搜索:' + res.value,
		icon: 'none'
	});

}
</script>

<style scoped>
</style>

3)在首页vue页面中使用组件CustomNavbar.vue

html 复制代码
<template>
  <custom-navbar/>
  <view class="content">
    <!-- 其他内容 -->
  </view>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import CustomNavbar from '../components/CustomNavbar.vue';
// ...... //
</script>
相关推荐
山河木马19 小时前
GPU自动处理专题3-NDC怎么映射成屏幕像素坐标(视口变换)
javascript·webgl·图形学
Appthing21 小时前
在 TanStack Start 中使用 VitePWA 的正确配置
javascript
前端炒粉1 天前
手写promise
java·前端·javascript
mONESY1 天前
Node.js fs 文件系统模块 四种读写方案全解析
javascript·后端
Larcher1 天前
从回调地狱到优雅异步:Node.js 文件操作进化史
javascript·后端·架构
荒诞英雄1 天前
从 17MB Vendor 大包到按需加载:Vite 多入口 Vue 组件库首屏优化实践
vue.js·vite
jsonbro1 天前
手把手带你实现发布订阅模式
前端·vue.js·设计模式
熊猫钓鱼>_>1 天前
Electron:当 Web 技术统治桌面
大数据·前端·javascript·人工智能·架构·electron·agent
江华森1 天前
Python 实现高德地图找房(三):地图可视化与高德 JS API
开发语言·javascript·python
谭光志1 天前
AI 是怎么操作浏览器的——browser use 实现原理
前端·javascript·ai编程