微信小程序自定义顶部导航栏并适配不同机型

< view class="topinfo" :style="{height: navHeight + 'px', paddingTop: statusBarHeight + 'px'}">
			< text class="left">定位定位定位定位</ text>
			< text class="title">御灵通优选</ text>
			< text class="right"></ text>
		</ view>



data() {
            return {
				navHeight:0,
				statusBarHeight:0,
            }
        }



onLoad() {
			// 获取手机系统信息
			const info = uni.getSystemInfoSync()
			// 设置状态栏高度(H5顶部无状态栏小程序有状态栏需要撑起高度)
			this.statusBarHeight = info.statusBarHeight
			// 除了h5 app mp-alipay的情况下执行
			// 获取胶囊的位置
			const menuButtonInfo = uni.getMenuButtonBoundingClientRect()
			// (胶囊底部高度 - 状态栏的高度) + (胶囊顶部高度 - 状态栏内的高度) = 导航栏的高度
			this.navHeight = (menuButtonInfo.bottom - info.statusBarHeight) + (menuButtonInfo.top - info.statusBarHeight)
			console.log(this.navHeight,'高度')
		},


.topinfo{
			width: 100%;
			background: #F24645;
			display: flex;
			justify-content: space-between;
			align-items: center;
			color: rgba(255, 255, 255, 1);
			
			.left{
				flex: 0 0 auto;
			}
			.right{
				flex: 1;
			}
			.title{
				font-size: 36rpx;
				flex: 1;
				text-align: center;
				overflow: hidden;
				text-overflow: ellipsis;
				white-space: nowrap;
			}
		}
### 创建兼容所有机型自定义顶部导航栏 #### 1. 移除默认导航栏样式 为了确保自定义导航栏能够完全控制布局,需先移除页面原有的默认导航栏。这可以通过配置 `pages.json` 文件中的 `"navigationStyle"` 属性为 `"custom"` 来实现[^4]。 ```json { "path": "pages/index/index", "style": { "navigationStyle": "custom" } } ``` #### 2. 动态计算导航栏高度 不同设备屏幕比例和是否存在刘海屏等因素会影响实际显示区域大小。因此建议通过 API 获取当前设备的安全区域内信息,据此调整自定义导航栏的高度以适应各种终端环境[^2]。 - **获取状态栏高度** 可调用微信小程序提供的 `getSystemInfoSync()` 接口读取系统信息对象内的 `statusBarHeight` 字段获得状态栏的具体数值。 - **测量胶囊按钮位置** 对于微信小程序而言,“返回”图标通常位于左上角靠近“胶囊”的地方;而右侧则放置着诸如菜单之类的交互控件。“胶囊”的尺寸及其距离顶部的距离决定了整个导航条目的最终呈现形式。利用 `selectComponent('#menuButtonRect')` 方法可方便地取得该组件的相关几何参数。 基于上述两个数据源,再加上开发者预设的内容部分(比如文字、图片等),便能精确设定出适合特定平台版本下的个性化头部区块了。 #### 3. 自定义导航栏HTML结构设计 构建一个简单的 HTML 结构用于承载自定义导航栏元素: ```html <view class="nav-bar"> <!-- 左侧返回箭头 --> <image src="/static/back.png" @click="handleBack"></image> <!-- 中间标题 --> <text>{{ title }}</text> <!-- 右边操作按钮 --> <button type="default">...</button> </view> ``` #### 4. CSS样式设置 针对不同场景应用相应的视觉效果,同时考虑到跨端一致性问题,推荐采用 Flexbox 布局方式简化开发流程提高代码复用率: ```css .nav-bar { display: flex; justify-content: space-between; align-items: center; padding-left: 30rpx; /* 考虑到左侧有返回键 */ height: var(--status-bar-height, 44px); /* 默认值可根据实际情况修改 */ } /* 如果是iPhone X及以上系列,则增加额外间距 */ @media (device-width: 375px) and (device-height: 812px), (device-width: 414px) and (device-height: 896px){ .nav-bar {padding-top: constant(safe-area-inset-top);} } ``` 注意这里使用了CSS变量(`--status-bar-height`)来存储动态变化的状态栏高度,使得后续维护更加便捷高效。另外还加入了媒体查询语句专门处理带有凹槽的设计方案(iPhoneX+),从而保证整体美观度不受影响。 #### 5. JavaScript逻辑编写 最后一步就是完善事件处理器函数以及初始化时必要的准备工作啦! ```javascript export default { data() { return { title: '首页', statusBarHeight: uni.getSystemInfoSync().statusBarHeight, menuButtonBoundingClientRect: null }; }, onLoad(){ const res = wx.getMenuButtonBoundingClientRect(); this.menuButtonBoundingClientRect = res; // 更新全局样式表里的变量值 document.documentElement.style.setProperty('--status-bar-height', `${this.statusBarHeight}px`); }, methods:{ handleBack(){ uni.navigateBack({ delta: 1 }); } } }; ``` 以上就是在uni-app框架内打造一款既实用又好看的响应式自定义导航栏全过程介绍。希望这些资料对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值