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

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

问题描述

当我们在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_9159090615 小时前
iOS 应用反调试技详解术 检测调试器的原理与防护实践
android·ios·小程序·https·uni-app·iphone·webview
00后程序员张18 小时前
iOS加固技术路线全面解析:Bitcode模式、源码模式与汇编模式对比及爱加密优势
android·汇编·ios·小程序·uni-app·cocoa·iphone
海纳百川·纳海川18 小时前
租房行业数字化:换个思路解决“老问题”
大数据·微信小程序·小程序
上海云盾-小余1 天前
CC 高频请求 + DDoS 流量攻击实战防御:全链路 WAF 流量清洗部署完整流程
网络·爬虫·安全·小程序·ddos
2501_916008891 天前
iOS IPA文件反编译与打包操作方法,拆包分析防护和加固打包
android·macos·ios·小程序·uni-app·cocoa·iphone
2301_810313181 天前
海外教育APP定制发展现状与开发选型核心要点解析
小程序·团队开发·软件需求
2501_915921432 天前
iOS手游安全反外挂解决方案:基于Xcode的加固混淆特色详解
android·安全·ios·小程序·uni-app·iphone·xcode
禾高网络2 天前
回收小程序在环保和商业领域发挥更大的作用
java·大数据·人工智能·小程序
2501_916007472 天前
iOS App分发教程 App Store、TestFlight 与 Ad Hoc 的配置与上传方法
android·ios·小程序·https·uni-app·iphone·webview
JasonMa1532 天前
从零搭建一个微信小程序活动管理平台(一):架构设计与技术选型
javascript·微信小程序·node.js