当前位置:   article > 正文

微信小程序中安全区域计算和适配_微信开发工具 获取安全区

微信开发工具 获取安全区

前言

自从iphoneX问世之后,因为iphoneX、iphoneXR和后续全面屏手机设备,因为物理Home键被底部小黑条代替了,这时候很多前端小伙伴在开发的过程都会遇到 “全面屏”和“非全面屏”的兼容性问题,普遍问题就是底部按钮或者选项卡与底部黑线重叠

解释

根据官方解释:

安全区域指的是一个可视窗口范围,处于安全区域的内容不受圆角(corners)、齐刘海(sensor housing)、小黑条(Home Indicator)的影响。

具体区域如图展示

适配方案

当前有效的解决方式有几种

  1. 使用已知底部小黑条高度34px/68rpx来适配

  1. 使用苹果官方推出的css函数env()、constant()适配

  1. 使用微信官方API,getSystemInfo()中的safeArea对象进行适配

使用已知底部小黑条高度34px/68rpx来适配

这种方式是根据实践得出,通过物理方式测出iPhone底部的小黑条(Home Indicator)高度是34px,实际在开发者工具选中真机获取到高度也是34px,所以直接根据该值,设置margin-bottom、padding-bottom、height也能实现。同时这样做要有一个前提,需要判断当前机型是需要适配安全区域的机型。

但是这种方案相对来说是不推荐使用的。比较是一个比较古老原始的方案

使用苹果官方推出的css函数env()、constant()适配

这种方案是苹果官方推荐使用env(),constant()来适配,开发者不需要管数值具体是多少。

env和constant是IOS11新增特性,有4个预定义变量:

  • safe-area-inset-left:安全区域距离左边边界的距离

  • safe-area-inset-right:安全区域距离右边边界的距离

  • safe-area-inset-top:安全区域距离顶部边界的距离

  • safe-area-inset-bottom :安全距离底部边界的距离

具体用法如下:

Tips: constant和env不能调换位置
  1. padding-bottom: constant(safe-area-inset-bottom); /*兼容 IOS<11.2*/
  2. padding-bottom: env(safe-area-inset-bottom); /*兼容 IOS>11.2*/

其实利用这个能解决大部分的适配场景了,但是有时候开发需要自定义头部信息,这时候就没办法使用css来解决了

使用微信官方API,getSystemInfo()中的safeArea对象进行适配

通过 wx.getSystemInfo获取到各种安全区域信息,解析出具体的设备类型,通过设备类型做宽高自适应,话不多说,直接上代码

代码实现
  1. const res = wx.getSystemInfoSync()
  2. const result = {
  3. ...res,
  4. bottomSafeHeight: 0,
  5. isIphoneX: false,
  6. isMi: false,
  7. isIphone: false,
  8. isIpad: false,
  9. isIOS: false,
  10. isHeightPhone: false,
  11. }
  12. const modelmes = result.model
  13. const system = result.system
  14. // 判断设备型号
  15. if (modelmes.search('iPhone X') != -1 || modelmes.search('iPhone 11') != -1) {
  16. result.isIphoneX = true;
  17. }
  18. if (modelmes.search('MI') != -1) {
  19. result.isMi = true;
  20. }
  21. if (modelmes.search('iPhone') != -1) {
  22. result.isIphone = true;
  23. }
  24. if (modelmes.search('iPad') > -1) {
  25. result.isIpad = true;
  26. }
  27. let screenWidth = result.screenWidth
  28. let screenHeight = result.screenHeight
  29. // 宽高比自适应
  30. screenWidth = Math.min(screenWidth, screenHeight)
  31. screenHeight = Math.max(screenWidth, screenHeight)
  32. const ipadDiff = Math.abs(screenHeight / screenWidth - 1.33333)
  33. if (ipadDiff < 0.01) {
  34. result.isIpad = true
  35. }
  36. if (result.isIphone || system.indexOf('iOS') > -1) {
  37. result.isIOS = true
  38. }
  39. const myCanvasWidth = (640 / 375) * result.screenWidth
  40. const myCanvasHeight = (1000 / 667) * result.screenHeight
  41. const scale = myCanvasWidth / myCanvasHeight
  42. if (scale < 0.64) {
  43. result.isHeightPhone = true
  44. }
  45. result.navHeight = result.statusBarHeight + 46
  46. result.pageWidth = result.windowWidth
  47. result.pageHeight = result.windowHeight - result.navHeight
  48. if (!result.isIOS) {
  49. result.bottomSafeHeight = 0
  50. }
  51. const capsuleInfo = wx.getMenuButtonBoundingClientRect()
  52. // 胶囊热区 = 胶囊和状态栏之间的留白 * 2 (保持胶囊和状态栏上下留白一致) * 2(设计上为了更好看) + 胶囊高度
  53. const navbarHeight = (capsuleInfo.top - result.statusBarHeight) * 4 + capsuleInfo.height
  54. // 写入胶囊数据
  55. result.capsuleInfo = capsuleInfo;
  56. // 安全区域
  57. const safeArea = result.safeArea
  58. // 可视区域高度 - 适配横竖屏场景
  59. const screenHeight = Math.max(result.screenHeight, result.screenWidth)
  60. const height = Math.max(safeArea.height, safeArea.width)
  61. // 状态栏高度
  62. const statusBarHeight = result.statusBarHeight
  63. // 获取底部安全区域高度(全面屏手机)
  64. if (safeArea && height && screenHeight) {
  65. result.bottomSafeHeight = screenHeight - height - statusBarHeight
  66. if (result.bottomSafeHeight < 0) {
  67. result.bottomSafeHeight = 0
  68. }
  69. }
  70. // 设置header高度
  71. result.headerHeight = statusBarHeight + navbarHeight
  72. // 导航栏高度
  73. result.navbarHeight = navbarHeight

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/448576
推荐阅读
相关标签
  

闽ICP备14008679号