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

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

问题描述

当我们在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用来获取系统信息

相关推荐
2501_915106323 小时前
iOS 抓不到包怎么办?从 HTTPS 代理排查到 TCP 数据流捕获的全链路解决方案
android·tcp/ip·ios·小程序·https·uni-app·iphone
游戏开发爱好者83 小时前
APP上架苹果应用商店经验教训与注意事项
android·ios·小程序·https·uni-app·iphone·webview
hyswl6664 小时前
数字货物搬家小程序
python·小程序
郭俊强5 小时前
小程序RSA、AES加密
小程序
Dest1ny-安全6 小时前
CTF入门:国内线上CTF比赛时间及部分题目资源
网络·安全·web安全·微信小程序·php
2501_916007476 小时前
苹果应用商店上架的系统逻辑,从产品开发到使用 开心上架 上架IPA 交付审核流程
android·ios·小程序·https·uni-app·iphone·webview
kyh10033811207 小时前
去水印微信小程序搭建
大数据·微信小程序·去水印·短视频去水印·去水印微信小程序
2501_916008897 小时前
Python抓包HTTPS详解:Wireshark、Fiddler、Charles等工具使用教程
python·ios·小程序·https·uni-app·wireshark·iphone
刻刻帝的海角7 小时前
uniapp引入qqmap-wx-jssdk实现微信小程序端获取用户当前位置
微信小程序·小程序·uni-app
2501_916008898 小时前
uni-app 上架到 App Store 的项目流程,构建、打包与使用开心上架(Appuploader)上传
android·ios·小程序·https·uni-app·iphone·webview