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

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

问题描述

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

相关推荐
沉默-_-4 小时前
微信小程序网络请求 wx.request 详解
网络·学习·微信小程序·小程序
2501_915909065 小时前
设置了 SSL Pinning 与双向 TLS 验证要怎么抓包
网络·网络协议·ios·小程序·uni-app·iphone·ssl
沉默-_-5 小时前
微信小程序页面配置详解
学习·微信小程序·apache·微信开发者工具
龙兵科技小付说9 小时前
不同价格的上门做饭小程序APP都有什么功能?
小程序·软件开发·上门做饭
2501_9160074711 小时前
如何查看 iOS 设备系统与硬件信息,iOS系统信息显示工具
android·ios·小程序·https·uni-app·iphone·webview
逆龙泰氽11 小时前
微信小程序开发04-1(小程序API)
微信小程序·小程序
说私域11 小时前
AI智能名片S2B2C商城小程序在微商中的应用与影响
大数据·人工智能·小程序·流量运营
苏苏哇哈哈12 小时前
微信小程序实现高性能动态配置水滴凹槽、凸起Tabbar 组件
微信小程序·小程序
逆龙泰氽12 小时前
微信小程序开发03(WXML语法)
微信小程序·小程序
2501_9160074712 小时前
iOS APP 开发,从项目创建、证书与描述文件配置、安装测试和IPA 上传
android·ios·小程序·https·uni-app·iphone·webview