微信小程序如何自定义导航栏,怎么确定导航栏及状态栏的高度?导航栏被刘海、信号图标给覆盖了怎么办?

声明:本文为了演示效果,颜色采用的比较显眼,可根据实际情况修改颜色

问题描述

当我们在JSON中将navigationStyle设置成custom后,当前页面的顶部导航栏就需要我们制作了,但出现了一下几个问题:

  1. 导航栏的高度该是多少?
  2. 导航栏被刘海、信号图标给覆盖了,就像下面

分析

整个顶部导航栏部分实际上是分为两部分的,分别是状态栏导航栏,所以需要对两部分都进行处理

解决方法

1. 导航栏高度该是多少?

安卓手机建议设置48px,iOS手机建议设置44px

怎么获取当前是iOS还是安卓

通过Apiwx.getSystemInfo()获取其中的system(操作系统及版本)字段,看其中是否包含iOS字符串

示例:

html 复制代码
<view style="background-color: blue;height: {{navBarHeight}}px;text-align: center;color: #ffffff;line-height: {{navBarHeight}}px;">导航栏</view>
<!-- 这里的样式比较简单,自行丰富 -->
js 复制代码
Page({
    data: {
        navBarHeight: 48
    },
    onLoad() {
        var that = this;
        wx.getSystemInfo({
            success(res) {
                let nav = 48; // 默认为48px
                // 判断是否是iOS
                if (res.system.indexOf('iOS') > -1) {
                    nav = 44
                }
                that.setData({
                    navBarHeight: nav
                })
            }
        })
    }
})

2. 导航栏被刘海、信号图标给覆盖了

这里的原因就是没有设置状态栏部分的高度,需要获取设备的状态栏高度

怎么获取?

还是通过Apiwx.getSystemInfo()获取其中的statusBarHeight(状态栏的高度,单位px)字段

示例:

html 复制代码
<view style="background-color: brown;height: {{statusBarHeight}}px;"></view>
<!-- 我这里是通过高度支撑的,也可以通过外边距等实现布局 -->
js 复制代码
Page({
    data: {
        statusBarHeight: 22
    },
    onLoad() {
        var that = this;
        wx.getSystemInfo({
            success(res) {
                that.setData({
                    statusBarHeight: res.statusBarHeight
                })
            }
        })
    }
})

汇总

html 复制代码
<view style="background-color: brown;height: {{statusBarHeight}}px;"></view>
<view style="background-color: blue;height: {{navBarHeight}}px;text-align: center;color: #ffffff;line-height: {{navBarHeight}}px;">导航栏</view>
js 复制代码
Page({
    data: {
        statusBarHeight: 22,
        navBarHeight: 48
    },
    onLoad() {
        var that = this;
        wx.getSystemInfo({
            success(res) {
                // 获取状态栏高度及是否是iOS设备
                let nav = 48; // 默认为48px
                // 判断是否是iOS
                if (res.system.indexOf('iOS') > -1) {
                    nav = 44
                }
                that.setData({
                    statusBarHeight: res.statusBarHeight,
                    navBarHeight: nav
                })
            }
        })
    }
})

扩展

wx.getSystemInfo用来获取系统信息

相关推荐
说私域32 分钟前
开源链动2+1模式、AI智能名片与S2B2C商城小程序:社群经济的数字化重构路径
人工智能·小程序·开源
peachSoda71 小时前
封装一个不同跳转方式的通用方法(跳转外部链接,跳转其他小程序,跳转半屏小程序)
前端·javascript·微信小程序·小程序
2501_915909062 小时前
iOS 26 文件管理实战,多工具组合下的 App 数据访问与系统日志调试方案
android·ios·小程序·https·uni-app·iphone·webview
云起SAAS3 小时前
ai周公解梦抖音快手微信小程序看广告流量主开源
微信小程序·小程序·ai编程·看广告变现轻·ai周公解梦
Jing_jing_X5 小时前
微信小程序开发踩坑记:从AI工具翻车到找到合适方案
人工智能·ai·小程序·产品运营·个人开发
一匹电信狗6 小时前
【MySQL】数据库的相关操作
linux·运维·服务器·数据库·mysql·ubuntu·小程序
weixin_lynhgworld10 小时前
旧物新生,从二手回收小程序开启绿色生活
小程序·生活·旧物回收
從南走到北12 小时前
智尚招聘求职小程序v1.0.23
微信小程序·小程序
2501_9159184116 小时前
掌握 iOS 26 App 运行状况,多工具协作下的监控策略
android·ios·小程序·https·uni-app·iphone·webview
知识分享小能手16 小时前
uni-app 入门学习教程,从入门到精通,uni-app基础扩展 —— 详细知识点与案例(3)
vue.js·学习·ui·微信小程序·小程序·uni-app·编程