微信小程序 uniapp - iPhoneX 刘海屏适配

一、js直接调用wx.getSystemInfoSync

Page({
  data: {
    isIPhoneX:false,
  },
  onLoad: function () {
    this.setData({
      "isIphoneX": this.isIphoneX()
    })
  },
  isIphoneX() {
   let res = wx.getSystemInfoSync();
   if (res.screenHeight == 812 || res.screenHeight == 896) {
      return true;
    } else {
      return false;
    }
  }
})

二、调用wx.getSystemInfoSync

wx.getSystemInfo({
      success: function (res) {
        var safeBottom = res.screenHeight - res.safeArea.bottom
        that.kBottomSafeHeight = safeBottom
        //根据安全高度判断
        if (safeBottom === 34) {
          that.globalData.isIPhoneX = true
          that.isIPhoneX = true
        }
        // // 根据 model 进行判断
        // if (res.model.search('iPhone X') != -1) {
        //   that.globalData.isIPhoneX = true
        //   that.isIPhoneX = true
        // }
        // // 或者根据 screenHeight 进行判断
        // if (res.screenHeight == 812 || res.screenHeight == 896) {
        //   that.isIPhoneX = true
        // }
      }
    })

版权声明:本文为zxh7770原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。