JS获取iOS iPhone安全区域SafeArea

苹果手机iPhone X及以上版本都带有刘海屏,14pro以上版本变成了所谓灵动岛,所以顶部和底部都有一个安全区域。

手机型号尺寸(pt)倍数屏幕(px)状态栏高度顶部安全距离底部安全距离
iPhone14Pro、iPhone15Pro、iPhone15393 * 85231179 * 255654pt59pt34pt(竖屏) / 21pt(横屏)
iPhone14ProMax iPhone15ProMax、iPhone15plus430 * 93231290 * 279654pt59pt34pt(竖屏) / 21pt(横屏)
iPhone12、iPhone12Pro、iPhone13、iPhone13Pro、iPhone14390 * 84431170 * 253247pt47pt34pt(竖屏) / 21pt(横屏)
iPhone12ProMax、iPhone13ProMax、iPhone14Plus428 * 92631284 * 277847pt47pt34pt(竖屏) / 21pt(横屏)
iPhone12mini、iPhone13mini375 * 81231125 * 243650pt50pt34pt(竖屏) / 21pt(横屏)
iPhoneXS Max、iPhone11ProMax414 * 89631242 * 268844pt44pt34pt(竖屏) / 21pt(横屏)
iPhoneX、iPhoneXS、iPhone 11 Pro375 * 81231125 * 243644pt44pt34pt(竖屏) / 21pt(横屏)
iPhoneXR、iPhone 11414 * 8962828* 179248pt48pt34pt(竖屏) / 21pt(横屏)
iPhone6 Plus、iPhone6s Plus、iPhone7 Plus、iPhone8 Plus414 * 73631242x220820pt20pt---
iPhone6、iPhone6s、iPhone7、iPhone8、iPhoneSE2、iPhoneSE3375 * 6672750 * 133420pt20pt---

在web网页中需要根据安全区域从而适当的调整布局,  那么如何获取顶部和底部安全区域的高度呢。直接上代码

const bottomAreaHeight = ref(0)
let safeAreaHeightValue = 0
let topSafeAreaHeight = 0
let bottomSafeAreaHeight = 0
onMounted(() => {
  // 获取底部安全区域的高度
  const isIphone = /iPhone/i.test(navigator.userAgent)

  if (isIphone) {
    safeAreaHeightValue = window.screen.height - window.innerHeight

    if (safeAreaHeightValue >= 44) {
      bottomSafeAreaHeight = 34
    }

    switch (safeAreaHeightValue) {
      case 93:
      case 59:
        topSafeAreaHeight = 59
        break

      case 81:
      case 47:
        topSafeAreaHeight = 47
        break

      case 84:
      case 50:
        topSafeAreaHeight = 50
        break

      case 78:
      case 44:
        topSafeAreaHeight = 44
        break

      case 82:
      case 48:
        topSafeAreaHeight = 48
        break

      case 20:
        topSafeAreaHeight = 20
        break

      default:
        break
    }
  }
  bottomAreaHeight.value = bottomSafeAreaHeight

 为什么是两种数值对应一个相同的值呢,主要原因是iOS端有可能设置Webview的忽略安全距离

import SwiftUI
import WebKit
struct ContentView: View {
    @State var url: String? = "https://www.baidu.com"
    var body: some View {
        SWKWebView(url: $url).edgesIgnoringSafeArea(.all)
        //top 93 bottom 59 all 59
    }
}

#Preview {
    ContentView()
}

拿iPhone15举例来说,edgesIgnoringSafeArea(.all)和edgesIgnoringSafeArea(.bottom),window.screen.height:852,innerHeight:793,差值为59,

edgesIgnoringSafeArea(.top)或者不设置时 "innerHeight":759,"screnHeight":852,差值为93。

另外需要注意设置edgesIgnoringSafeArea(.top)和all时顶部如果有返回按钮啥的注意距离顶部的距离,否则有可能造成跑到状态来上去了

  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值