小程序适配IOS底部小黑条

1、IOS底部小黑条高34px进行适配

<view class="container-px" style="padding-bottom: {{isIOS ? '68rpx' : 0}};">
  <view class="container-wrap"></view>
</view>

2、使用css

兼容ios<11.2
padding-bottom: constant(safe-area-inset-bottom); 底部安全区域812 - 778 = 34
兼容ios>11.2
padding-bottom: env(safe-area-inset-bottom);
env() 跟 constant() 需要同时存在,而且顺序不能换

<view class="container-css-fit">
  <view class="container-wrap"></view>
</view>
.container-css-fit{
  box-sizing: border-box;
  width: 100%;
  height: 100vh;
  /* 兼容ios<11.2 */
  padding-bottom: constant(safe-area-inset-bottom);  /* 底部安全区域812 - 778 = 34*/
  /* 兼容ios>11.2 */
  padding-bottom: env(safe-area-inset-bottom);
  /* env() 跟 constant() 需要同时存在,而且顺序不能换 */
}
.container-wrap{
  width: 100%;
  height: 100%;
  background-color: plum;
}

3、使用getSystemInfoSync计算安全区域

<view class="container-system" style="{{result.bottomSafeHeight ? ('padding-bottom: ' + result.bottomSafeHeight + 'px') : ''}}">
  <view class="container-wrap"></view>
</view>

const res = wx.getSystemInfoSync()
const result = {
…res,
bottomSafeHeight: 0,
isIphoneX: false,
isMi: false,
isIphone: false,
isIpad: false,
isIOS: false,
isHeightPhone: false,
}
const modelmes = result.model
const system = result.system
// 判断设备型号
if (modelmes.search(‘iPhone X’) != -1 || modelmes.search(‘iPhone 11’) != -1) {
result.isIphoneX = true;
}
if (modelmes.search(‘MI’) != -1) {
result.isMi = true;
}
if (modelmes.search(‘iPhone’) != -1) {
result.isIphone = true;
}
if (modelmes.search(‘iPad’) > -1) {
result.isIpad = true;
}
let screenWidth = result.screenWidth
let screenHeight = result.screenHeight
// 宽高比自适应
screenWidth = Math.min(screenWidth, screenHeight)
screenHeight = Math.max(screenWidth, screenHeight)
const ipadDiff = Math.abs(screenHeight / screenWidth - 1.33333)
if (ipadDiff < 0.01) {
result.isIpad = true
}
if (result.isIphone || system.indexOf(‘iOS’) > -1) {
result.isIOS = true
}
const myCanvasWidth = (640 / 375) * result.screenWidth
const myCanvasHeight = (1000 / 667) * result.screenHeight
const scale = myCanvasWidth / myCanvasHeight
if (scale < 0.64) {
result.isHeightPhone = true
}
result.navHeight = result.statusBarHeight + 46
result.pageWidth = result.windowWidth
result.pageHeight = result.windowHeight - result.navHeight
if (!result.isIOS) {
result.bottomSafeHeight = 0
}
const capsuleInfo = wx.getMenuButtonBoundingClientRect() // 获取菜单按钮(右上角胶囊按钮)的布局位置信息。坐标信息以屏幕左上角为原点
// 胶囊热区 = 胶囊和状态栏之间的留白 * 2 (保持胶囊和状态栏上下留白一致) * 2(设计上为了更好看) + 胶囊高度
const navbarHeight = (capsuleInfo.top - result.statusBarHeight) * 4 + capsuleInfo.height
// 写入胶囊数据
result.capsuleInfo = capsuleInfo;
// 安全区域
const safeArea = result.safeArea
// 可视区域高度 - 适配横竖屏场景
const height = Math.max(safeArea.height, safeArea.width)
// 状态栏高度
const statusBarHeight = result.statusBarHeight
// 获取底部安全区域高度(全面屏手机)
if (safeArea && height && screenHeight) {
result.bottomSafeHeight = screenHeight - height - statusBarHeight
if (result.bottomSafeHeight < 0) {
result.bottomSafeHeight = 0
}
}
// 设置header高度
result.headerHeight = statusBarHeight + navbarHeight
// 导航栏高度
result.navbarHeight = navbarHeight

  • 39
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
苹果底部小横条(iPhone X 及以上机型的底部指示条)的高度为 34px,它会遮挡小程序底部部分内容,因此需要进行。 在支付宝小程序中,可以使用 `safe-area-view` 组件来苹果底部小横条。`safe-area-view` 组件可以根据设备的实际情况自动调整布局,使得小程序内容不被遮挡。 具体实现步骤如下: 1. 在需要的页面的 `.axml` 文件中,使用 `safe-area-view` 标签包裹需要的内容。 2. 在 `safe-area-view` 标签中设置 `padding-bottom` 属性,属性值为 `34rpx`,表示底部留出 34px 的安全区域。 下面是一个示例代码: ```html <safe-area-view style="background-color: white;"> <view style="height: 100vh; padding-bottom: 34rpx;"> <!-- 需要的内容 --> </view> </safe-area-view> ``` 注意:使用 `safe-area-view` 组件进行时,需要将的内容放在 `safe-area-view` 标签内,而不能直接放在页面根节点下。另外,需要注意 `padding-bottom` 的值,不同的设备可能需要设置不同的值来底部小横条。 另外,如果小程序中有固定在底部的 tabbar,也需要进行,可以在 `.axml` 文件中使用 `tabbar` 标签,并设置 `safe` 属性为 `true`,表示在底部留出安全区域。 下面是一个示例代码: ```html <tabbar safe="true"> <tabbar-item text="首页" icon="home" url="/pages/index/index"></tabbar-item> <tabbar-item text="分类" icon="category" url="/pages/category/category"></tabbar-item> <tabbar-item text="购物车" icon="cart" url="/pages/cart/cart"></tabbar-item> <tabbar-item text="我的" icon="user" url="/pages/user/user"></tabbar-item> </tabbar> ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值