【小程序】wx.getSystemInfo() 、自定义状态栏

wx.getSystemInfo(Object object) / 自定义状态栏

wx.getSystemInfo(Object object)

获取系统信息

参数
Object object(红色框为常用)

在这里插入图片描述

object.success 回调函数(列举常用的几个,具体看官方文档)
Object res

在这里插入图片描述

示例代码
wx.getSystemInfo({
    success({
        console.log(res.windowWidth);
        console.log(res.windowHeight);
    })
})

自定义状态栏

首先,了解下图:
在这里插入图片描述

第一步

1、开启配置自定义顶部
在这里插入图片描述

{ 
"window": 
     { "navigationStyle":"custom" }
}

在app.json的文件window配置"navigationStyle": "custom"属性即可

第二步

2.自定义顶部计算原理

2.1 获取系统状态栏的高度和屏幕宽度

使用wx.getSystemInfo这个函数获取系统状态栏的高度和屏幕宽度。
在这里插入图片描述
2.2 获取右上角胶囊位置信息

使用wx.getMenuButtonBoundingClientRect()方法获取右上角胶囊位置信息。
关键问题在于自定义胶囊的上、下和左边距。
在这里插入图片描述
2.3 自定义顶部距离计算代码

wx.getSystemInfo({ // 获取系统信息
      success: e => {
        this.globalData.StatusBar = e.statusBarHeight;  // 手机系统状态栏的高度
        let capsule = wx.getMenuButtonBoundingClientRect(); // 获取菜单按钮(右上角胶囊按钮)的布局位置信息。坐标信息以屏幕左上角为原点。
		if (capsule) {
		 	this.globalData.Custom = capsule;
			this.globalData.CustomBar = capsule.bottom + capsule.top - e.statusBarHeight; // 获取自定义状态栏高度
		} else {
			this.globalData.CustomBar = e.statusBarHeight + 50;
		}
      }
    })
项目中使用:

在这里插入图片描述

"window":{
    "navigationStyle": "custom",
    "navigationBarBackgroundColor": "#7cab99",
    "navigationBarTitleText": "定旅",
    "navigationBarTextStyle":"white"
}

在这里插入图片描述

App({
    onLaunch: function() {
         wx.getSystemInfo({ // 获取系统信息
      success: e => {
        this.globalData.StatusBar = e.statusBarHeight;  // 手机系统状态栏的高度
        let capsule = wx.getMenuButtonBoundingClientRect(); // 获取菜单按钮(右上角胶囊按钮)的布局位置信息。坐标信息以屏幕左上角为原点。
		if (capsule) {
		 	this.globalData.Custom = capsule;
			this.globalData.CustomBar = capsule.bottom + capsule.top - e.statusBarHeight; // 获取自定义状态栏高度
		} else {
			this.globalData.CustomBar = e.statusBarHeight + 50;
		}
      }
    })
    }
})

配合Color UI 使用自定义状态栏(导航栏)

在这里插入图片描述

使用自定义导航栏

导航栏作为常用组件有做简单封装,当然你也可以直接复制代码结构自己修改,达到个性化目的。

App.js 获得系统信息
 onLaunch: function() {
    wx.getSystemInfo({
      success: e => {
        this.globalData.StatusBar = e.statusBarHeight;
        let custom = wx.getMenuButtonBoundingClientRect();
        this.globalData.Custom = custom;  
        this.globalData.CustomBar = custom.bottom + custom.top - e.statusBarHeight;
      }
    })
},
App.json 配置取消系统导航栏,并全局引入组件
"window": {
	"navigationStyle": "custom"
},
"usingComponents": {
    "cu-custom":"/colorui/components/cu-custom"
}
page.wxml 页面可以直接调用了
<cu-custom bgColor="bg-gradual-pink" isBack="{{true}}">
	<view slot="backText">返回</view>
	<view slot="content">导航栏</view>
</cu-custom>

在这里插入图片描述

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
wx.getSystemInfo是一个用于获取系统信息的方法。它有两种调用方式:wx.getSystemInfowx.getSystemInfoAsync。其中,wx.getSystemInfo是异步的调用格式,但是是同步返回;而wx.getSystemInfoAsync是异步获取系统信息的方式。 该方法返回的结果包含了一些属性,其中包括success、fail和complete。success是接口调用成功时的回调函数,fail是接口调用失败时的回调函数,complete是接口调用结束时的回调函数,无论成功还是失败都会执行。 根据引用的内容,如果你在适配iPhone X屏幕时遇到问题,你可以使用wx.getSystemInfo来获取系统信息,从而更好地适配iPhone X的屏幕。<span class="em">1</span><span class="em">2</span> #### 引用[.reference_title] - *1* [微信小程序开发之——获取系统信息](https://blog.csdn.net/Calvin_zhou/article/details/120504257)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [微信小程序适配 iPhone X 总结](https://download.csdn.net/download/weixin_38624519/16209098)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值