uni-app iPhoneX以上屏幕安全区像素偏差3-5个px 全面屏手机均存在这种问题

uni-app iPhoneX以上屏幕安全区像素偏差3-5个px 全面屏手机均存在这种问题

开发环境 版本号 项目创建方式
Mac 10.15.8 HBuilderX

产品分类:uniapp/App

测试过的手机:

无错误型号 redmi5A iPhone8 iPhonese 有错误型号 iPhonemini iPhone11 iPhone12 iPhone12 pro

示例代码:

```html <template> <view class="content"> <scroll-view :scroll-x="true" style="white-space: nowrap;margin-left: 20rpx; width: 730rpx; height: 100rpx ; background-color: #0077AA; padding-top: 200rpx;" :show-scrollbar="true" :scroll-into-view="scrollintoview" :scroll-with-animation="true" id="tabtop"> <view style="width: 145rpx; display: inline-block;" v-for="(item,index) in tabBars" :key="item.id" :id="'tab'+index" :class="tabIndex === index ? 'selectText l-color-main' : '' " @click="changeTab(index)">{{item.title}}</view> </scroll-view>
    <swiper style="background-color: #007AFF;   width: 750rpx; height: 490px;" [@change](/user/change)="onChangeTab" :current="current">  
        <swiper-item v-for="(item,index) in tabBars" :key="item.id">  
            <text>1</text>  
        </swiper-item>  

    </swiper>  
    <!-- <block v-for="item,index in list" :key="index">  
        <commenList :item="item" :index="index" @followed="followed" @doSupport="doSupport"></commenList>  
        <commonHorizontalLine></commonHorizontalLine>  
    </block> -->  

</view>  
</template> <script> import commenList from "@/component/common/common_list/common_list.vue" import commonHorizontalLine from "@/component/common/common_horizontal_line/common_horizontal_line.vue" export default { components: { commenList, commonHorizontalLine }, data() { return { tabIndex: 0, current: 0, scrollintoview: '', tabBars: [{ id: 0, title: "栏目0" }, { id: 1, title: "栏目1" }, { id: 2, title: "栏目2" }, { id: 3, title: "栏目3" }, { id: 4, title: "栏目4" }, { id: 5, title: "栏目5" }, { id: 6, title: "栏目6" }, { id: 7, title: "栏目7" }, { id: 8, title: "栏目8" }, { id: 9, title: "栏目9" }, { id: 10, title: "栏目10" }], list: [{ cname: "昵称1", avatar: "/static/logo.png", post_time: "2021年02月26日13:02:01", is_follow: false, title: "文章的标题", title_pic: "/static/6acec660-4f31-11eb-a16f-5b3e54966275.jpg", support: { type: "support", support_count: 1, unsupport_count: 2 }, comment_count: 2, share_count: 2 }, { cname: "昵称2", avatar: "/static/logo.png", post_time: "2021年02月26日13:02:01", is_follow: false, title: "文章的标题", title_pic: "/static/6acec660-4f31-11eb-a16f-5b3e54966275.jpg", support: { type: "unsupport", support_count: 1, unsupport_count: 2 }, comment_count: 2, share_count: 2 }, { cname: "昵称3", avatar: "/static/logo.png", post_time: "2021年02月26日13:02:01", is_follow: false, title: "文章的标题", title_pic: null, support: { type: null, support_count: null, unsupport_count: 0 }, comment_count: null, share_count: null }, { cname: "昵称3", avatar: "/static/logo.png", post_time: "2021年02月26日13:02:01", is_follow: false, title: "文章的标题", title_pic: null, support: { type: "unsupport", support_count: null, unsupport_count: 0 }, comment_count: null, share_count: null } ] } }, onLoad() { }, methods: { changeTab(index) { this.tabIndex = index, this.scrollintoview = 'tab' + index this.current = index }, onChangeTab(e) { this.tabIndex = e.detail.current this.scrollintoview = 'tab' + e.detail.current }, // 关注功能 followed(index) { this.list[index].is_follow = true uni.showToast({ title: "关注成功" }) }, // 顶踩操作 doSupport(data) { if (data.type == 'unsupport' && this.list[data.index].support.type != 'unsupport') { this.list[data.index].support.unsupport_count = this.list[data.index].support.unsupport_count + 1 this.list[data.index].support.support_count = this.list[data.index].support.support_count - 1 if (this.list[data.index].support.support_count < 0) { this.list[data.index].support.support_count = 0 } uni.showToast({ title: "踩了一下" }) } if (data.type == 'support' && this.list[data.index].support.type != 'support') { this.list[data.index].support.unsupport_count = this.list[data.index].support.unsupport_count - 1 this.list[data.index].support.support_count = this.list[data.index].support.support_count + 1 if (this.list[data.index].support.unsupport_count < 0) { this.list[data.index].support.unsupport_count = 0 } uni.showToast({ title: "顶了一下" }) } this.list[data.index].support.type = data.type; } }, onLoad() { }, onReady() { uni.getSystemInfo({ success: function(res) { console.log(res.windowHeight); } }); const query = uni.createSelectorQuery().in(this); query.select('#tabtop').boundingClientRect(data => { console.log("得到布局位置信息" + JSON.stringify(data)); console.log("节点离页面顶部的距离为" + data.top); }).exec(); } } </script> <style> .content { display: flex; flex-direction: column; align-items: center; justify-content: center; } /* 禁止IOS 横向划动出现滚动条 */ /deep/.uni-scroll-view::-webkit-scrollbar { display: none; } .selectText { font-weight: bold; font-size: 42rpx; } </style>

<h2>操作步骤:</h2>
1. 获取屏幕的安全高度
```javascript
uni.getSystemInfo({  
    success: function(res) {  
        console.log(res.windowHeight);  
    }  
});
  1. 获取第一个元素scroll-view的bottom scroll-view元素的代码
<scroll-view :scroll-x="true" style="white-space: nowrap;margin-left: 20rpx;  width: 730rpx; height: 100rpx ; background-color: #0077AA; padding-top: 200rpx;"  
         :show-scrollbar="true" :scroll-into-view="scrollintoview" :scroll-with-animation="true" id="tabtop">  
            <view style="width: 145rpx; display: inline-block;" v-for="(item,index) in tabBars" :key="item.id" :id="'tab'+index"  
             :class="tabIndex === index ? 'selectText l-color-main' : '' " @click="changeTab(index)">{{item.title}}</view>  
        </scroll-view>  
  1. 获取scroll-view距离顶部的距离
const query = uni.createSelectorQuery().in(this);  
query.select('#tabtop').boundingClientRect(data => {  
    console.log("得到布局位置信息" + JSON.stringify(data));  
    console.log("节点离页面顶部的距离为" + data.top);  
}).exec();
  1. 通过console.log的输出 手工进行计算
<swiper style="background-color: #007AFF;   width: 750rpx; height: 490px;" [@change](/user/change)="onChangeTab" :current="current">  
    <swiper-item v-for="(item,index) in tabBars" :key="item.id">  
        <text>1</text>  
    </swiper-item>  
</swiper>

更多关于uni-app iPhoneX以上屏幕安全区像素偏差3-5个px 全面屏手机均存在这种问题的实战教程也可以访问 https://www.itying.com/category-93-b0.html

10 回复

来人啊

更多关于uni-app iPhoneX以上屏幕安全区像素偏差3-5个px 全面屏手机均存在这种问题的实战教程也可以访问 https://www.itying.com/category-93-b0.html


顶一下

我以为官方来回复我了呢

回复 大叔刘: 来了,测试确实有问题

回复 大叔刘: 有其他人顶,官方看到的几率更大,自己顶看到的几率更小

问题确认,已加分,后续修复
临时解决办法:

使用 css 来布局,不依赖 js 计算。
判断 ios 机型和系统版本自行修正高度(需要考虑后续修复的影响)

Nvue页面(不能使用%比)如何用css设置scroll-view 或swiper的高度?

回复 DCMarvel: 用 flex 布局

此问题已在 HX 3.1.6 + 版本修复

针对iPhoneX及以上全面屏设备出现的3-5px安全区偏差问题,这是iOS系统对刘海屏设备的特殊处理导致的。以下是解决方案:

  1. 使用官方安全区适配方案:
/* 在App.vue中添加全局样式 */
page {
    padding-top: constant(safe-area-inset-top);
    padding-top: env(safe-area-inset-top);
    padding-bottom: constant(safe-area-inset-bottom);
    padding-bottom: env(safe-area-inset-bottom);
}
  1. 对于特定组件,可以这样处理:
<view class="safe-area">
    <!-- 内容 -->
</view>

<style>
.safe-area {
    padding-bottom: constant(safe-area-inset-bottom);
    padding-bottom: env(safe-area-inset-bottom);
}
</style>
  1. 在JS中获取安全区高度:
uni.getSystemInfo({
    success(res) {
        const safeArea = res.safeArea;
        console.log('安全区域信息:', safeArea);
    }
});
  1. 对于滚动组件,建议使用padding代替固定高度:
scroll-view {
    padding-bottom: env(safe-area-inset-bottom);
}
回到顶部