微信小程序适配iphonex iphone11底部tabbar

需求:由于iphonex及之后的机型底部有一根横线,导致tabbar比原来提升了一段高度。根据底部tabbar提升的高度,计算出页面需要在底部给整个tabbar留出的位置高度。
PS:微信开发者工具iphonex与实际真机测试效果不同,底部并没有底部提升。

iphone11截图
app.js调用 wx.getSystemInfo接口获取页面信息,判断底部提升高度。
官网文档:获取系统信息

如图所示
在这里插入图片描述

底部提升的高度 = screenHeight - safeArea.bottom
tabbar为固定高度57px
app.js

  globalData: {
    bottomLift: 0,
  },
  onLaunch: function(options) {
    this.getDeviceSize().then(res => {
      const {bottomLift} = res
      this.globalData.bottomLift = bottomLift
    })
  },
  // 获取设备信息
  getDeviceSize: function() {
    return new Promise((resolve, reject) => {
      wx.getSystemInfo({
        success: function(res) {
          // console.log(res)
          const {screenHeight, safeArea} = res
          const {bottom} = safeArea
          const bottomLift = screenHeight - bottom
          resolve({bottomLift})
        }
      })
    })
  },

使用tabbar的页面

//js
Page({
  data: {
    bottomLift: 0,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let {bottomLift} = app.globalData
    if(bottomLift>=0){
      app.getDeviceSize().then(res => {
        const {bottomLift} = res
        this.setData({
          bottomLift
        })
      })
    }else{
      this.setData({
        bottomLift
      })
    }
  }
})
//wxss
.wrap{ 
  min-height: calc(100vh - 57px); 
  box-sizing: border-box;
}
//wxml
<view class='wrap' style="padding-bottom: {{57 + bottomLift}}px">
  //页面底部留出高度 = tabbar高度 + 提升高度
  ...
</view>

转载于:https://www.jianshu.com/p/469fc45c9157

  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值