iPhoneX页面安全区域与内容重叠问题

本文详细介绍了iPhoneX的屏幕尺寸及适配方案,包括安全区域(safearea)的概念、viewport-fit的应用、高度统计技巧以及媒体查询的使用。针对不同场景提供了具体的CSS设置案例。

转载自:https://www.cnblogs.com/lolDragon/p/7795174.html

 

1.  iPhoneX的介绍

 

屏幕尺寸

 

我们熟知的iPhone系列开发尺寸概要如下:

 

 

△ iPhone各机型的开发尺寸

 

转化成我们熟知的像素尺寸:

 

 

△ 每个机型的多维度尺寸

 

倍图其实就是像素尺寸和开发尺寸的倍率关系,但这只是外在的表现。倍图核心的影响因素在于PPI(DPI),了解屏幕密度与各尺寸的关系有助于我们深度理解倍率的概念:《基础知识学起来!为设计师量身打造的DPI指南》

 

iPhone8在本次升级中,屏幕尺寸和分辨率都遗传了iPhone6以后的优良传统;

 

然而iPhone X 无论是在屏幕尺寸、分辨率、甚至是形状上都发生了较大的改变,下面以iPhone 8作为参照物,看看到底iPhone X的适配我们要怎么考虑。

 

我们看看iPhone X尺寸上的变化:

 

 

2.  iPhoneX的适配---安全区域(safe area)

 

苹果对于 iPhone X 的设计布局意见如下:

 

 

 

核心内容应该处于 Safe area 确保不会被设备圆角(corners),传感器外壳(sensor housing,齐刘海) 以及底部的 Home Indicator 遮挡。也就是说 我们设计显示的内容应该尽可能的在安全区域内;

 

3.  iPhoneX的适配---适配方案viewport-fit

 

    3.1  PhoneX的适配,在iOS 11中采用了viewport-fit的meta标签作为适配方案;viewport-fit的默认值是auto。

 

    viewport-fit取值如下:

 auto:    默认:viewprot-fit:contain;页面内容显示在safe area内
 cover:    viewport-fit:cover,页面内容充满屏幕

 

 

 

viewport-fit meta标签设置(cover时)

<meta name="viewport" content="width=device-width,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover">

 

 

    3.2  css constant()函数 与safe-area-inset-top & safe-area-inset-left & safe-area-inset-right & safe-area-inset-bottom的介绍

 

 

 

 

 

 

   如上图所示 在iOS 11中的WebKit包含了一个新的CSS函数constant(),以及一组四个预定义的常量:safe-area-inset-left, safe-area-inset-right, safe-area-inset-top和 safe-area-inset-bottom。当合并一起使用时,允许样式引用每个方面的安全区域的大小。

 

    3.1当我们设置viewport-fit:contain,也就是默认的时候时;设置safe-area-inset-left, safe-area-inset-right, safe-area-inset-top和 safe-area-inset-bottom等参数时不起作用的。

 

    3.2当我们设置viewport-fit:cover时:设置如下

body {
    padding-top: constant(safe-area-inset-top);   //为导航栏+状态栏的高度 88px            
    padding-left: constant(safe-area-inset-left);   //如果未竖屏时为0                
    padding-right: constant(safe-area-inset-right); //如果未竖屏时为0                
    padding-bottom: constant(safe-area-inset-bottom);//为底下圆弧的高度 34px       
}

 

 

4.   iPhoneX的适配---高度统计

 

    viewport-fit:cover + 导航栏

 

  

 

 

5.iPhoneX的适配---媒体查询

 

注意这里采用的是690px(safe area高度),不是812px;

@media only screen and (width: 375px) and (height: 690px){
    body {
        background: blue;
    }
}

 

 

6.iphoneX viewport-fit  问题总结

 

1.关于iphoneX 页面使用了渐变色时;如果viewport-fit:cover;

 

    1.1在设置了背景色单色和渐变色的区别,如果是单色时会填充整个屏幕,如果设置了渐变色 那么只会更加子元素的高度去渲染;而且页面的高度只有690px高度,上面使用了padding-top:88px;

 

  

 

body固定为:

<body><div class="content">this is subElement</div></body>

1.单色时:

       * {
           padding: 0;
           margin: 0;        
       }        
       body {
           background:green;
           padding-top: constant(safe-area-inset-top); //88px            
           /*padding-left: constant(safe-area-inset-left);*/            
           /*padding-right: constant(safe-area-inset-right);*/            
           /*padding-bottom: constant(safe-area-inset-bottom);*/        
       }

 

 

2.渐变色

       * {
           padding: 0;
           margin: 0;
       }
       body {
           background:-webkit-gradient(linear, 0 0, 0 bottom, from(#ffd54f), to(#ffaa22));
           padding-top: constant(safe-area-inset-top); //88px
           /*padding-left: constant(safe-area-inset-left);*/
           /*padding-right: constant(safe-area-inset-right);*/
           /*padding-bottom: constant(safe-area-inset-bottom);*/
       }

 

解决使用渐变色 仍旧填充整个屏幕的方法;CSS设置如下

 

<!DOCTYPE html>
<html>
<head>
   <meta name="viewport" content="initial-scale=1, viewport-fit=cover">
   <title>Designing Websites for iPhone X: Respecting the safe areas</title>
   <style>        * {
       padding: 0;
       margin: 0;
   }
   html, body {
       height: 100%;
   }
   body {
       padding-top: constant(safe-area-inset-top);
       padding-left: constant(safe-area-inset-left);
       padding-right: constant(safe-area-inset-right);
       padding-bottom: constant(safe-area-inset-bottom);
   }
   .content {
       background: -webkit-gradient(linear, 0 0, 0 bottom, from(#ffd54f), to(#ffaa22));
       width: 100%;
       height: 724px;
   }    </style>
</head>
<body>
<div class="content">this is subElement</div>
</body>
</html> 

 

2.页面元素使用了固定定位的适配即:{position:fixed;}

 

        2.1 子元素页面固定在底部时;使用viewport-fit:contain时;可以看到bottom:0时只会显示在安全区域内;

 

 

 

<!DOCTYPE html>
<html>
<head>
   <meta name="viewport" content="initial-scale=1">
   <!--<meta name="viewport" content="initial-scale=1, viewport-fit=cover">-->
   <title>Designing Websites for iPhone X: Respecting the safe areas</title>
   <style>
       * {
           padding: 0;
           margin: 0;
       }
       /*html,body {*/
           /*height: 100%;*/
       /*}*/
       body {
           background: grey;
           /*padding-top: constant(safe-area-inset-top);*/
           /*padding-left: constant(safe-area-inset-left);*/
           /*padding-right: constant(safe-area-inset-right);*/
           /*padding-bottom: constant(safe-area-inset-bottom);*/
       }
       .top {
           width: 100%;
           height: 44px;
           background: purple;
       }
       .bottom {
           position: fixed;
           bottom: 0;
           left: 0;
           right: 0;
           height: 44px;
           color: black;
           background: green;
       }
   </style>
</head>
<body>
   <div class="top">this is top</div>
   <div class="bottom">this is bottom</div>
</body>
</html>

 

 

 

 

2.1 子元素页面固定在底部时;使用viewport-fit:cover时;可以看到bottom:0时只会显示在安全区域内;

 

 

 

 

 

 

* {
    padding: 0;
    margin: 0;
}
/*html,body {*/
    /*height: 100%;*/
/*}*/
body {
    background: grey;
    padding-top: constant(safe-area-inset-top);
    /*padding-left: constant(safe-area-inset-left);*/
    /*padding-right: constant(safe-area-inset-right);*/
    /*padding-bottom: constant(safe-area-inset-bottom);*/
}
.top {
    width: 100%;
    height: 44px;
    background: purple;
}
.bottom {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: 44px;
    color: black;
    background: green;
}

 

 

添加html,body   {width:100%;heigth:100%}

 

 

 

 

 

图1:

* {
           padding: 0;
           margin: 0;
       }
       html,body {
           height: 100%;
       }
       body {
           background: grey;
           padding-top: constant(safe-area-inset-top);
           padding-left: constant(safe-area-inset-left);
           padding-right: constant(safe-area-inset-right);
           padding-bottom: constant(safe-area-inset-bottom);
       }
       .top {
           width: 100%;
           height: 44px;
           background: purple;
       }
       .bottom {
           position: fixed;
           bottom: 0;
           left: 0;
           right: 0;
           height: 44px;
           color: black;
           background: green;
       }

 

图2:

 * {
           padding: 0;
           margin: 0;
       }
       html,body {
           height: 100%;
       }
       body {
           background: grey;
           padding-top: constant(safe-area-inset-top);
           padding-left: constant(safe-area-inset-left);
           padding-right: constant(safe-area-inset-right);
           /*padding-bottom: constant(safe-area-inset-bottom);*/
       }
       .top {
           width: 100%;
           height: 44px;
           background: purple;
       }
       .bottom {
           position: fixed;
           bottom: 0;
           left: 0;
           right: 0;
           height: 44px;
           color: black;
           background: green;
       }

 

2.3 关于alertView弹框 遮罩层的解决方案

 

 

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <!--<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">-->
   <meta name="viewport" content="width=device-width,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover">
   <meta http-equiv="pragma" content="no-cache">
   <meta http-equiv="cache-control" content="no-cache">
   <meta http-equiv="expires" content="0">
   <title>alertView</title>
   <script data-res="eebbk">
       document.documentElement.style.fontSize = window.screen.width / 7.5 + 'px';
   </script>
   <style>
       * {
           margin: 0;
           padding: 0;
       }
       html,body {
           width: 100%;
           height: 100%;
       }
       body {
           font-size: 0.32rem;
           padding-top: constant(safe-area-inset-top);
           padding-left: constant(safe-area-inset-left);
           padding-right: constant(safe-area-inset-right);
           padding-bottom: constant(safe-area-inset-bottom);
       }
       .content {
           text-align: center;
       }
       .testBut {
           margin: 50px auto;
           width: 100px;
           height: 44px;
           border: 1px solid darkgray;
           outline:none;
           user-select: none;
           background-color: yellow;
       }
   </style>
   <link href="alertView.css" rel="stylesheet" type="text/css">
</head>
<body>
   <section class="content">
       <button class="testBut" onclick="showLoading()">弹框加载</button>
   </section>
   <script type="text/javascript" src="alertView.js"></script>
   <script>
       function showLoading() {
           UIAlertView.show({
               type:"input",
               title:"温馨提示",              //标题
               content:"VIP会员即将到期",     //获取新的
               isKnow:false
           });
           var xx = new UIAlertView();
          console.log(xx);
       }
   </script>
</body>
</html>

 

 

 

 

 

转载于:https://www.cnblogs.com/theWayToAce/p/7942360.html

<think>好的,我现在要帮用户解决微信小程序中隐藏导航栏后页面内容状态栏重叠问题。首先,我需要回顾一下用户提到的引用内容,看看有没有相关的信息可以借鉴。 用户提供了三个引用,其中引用2提到了通过计算状态栏高度和导航栏高度来自定义导航栏组件。引用3则讨论了自定义导航栏的好处和实现方法,包括创建组件并在页面中引入。而引用1则涉及导航栏被键盘撑出屏幕的问题,虽然主要问题不同,但提到的调整页面位置的方法可能有参考价值。 用户的问题是隐藏导航栏后,页面内容状态栏重叠。隐藏导航栏通常意味着使用自定义导航栏,或者关闭原生的导航栏显示。这时候,页面内容可能会上移到状态栏的位置,导致遮挡。因此,解决方案的关键在于正确计算状态栏的高度,并在页面布局中预留出相应的空间。 首先,我需要确认如何隐藏原生导航栏。根据微信小程序的文档,可以在页面的json配置中设置"navigationStyle": "custom",这样就会隐藏默认的导航栏。但这样做后,页面内容会从屏幕顶部开始,可能被状态栏覆盖。 接下来,解决方案需要获取状态栏的高度。根据引用2中的代码,可以通过wx.getSystemInfo获取statusBarHeight,这个值就是状态栏的高度。然后,在页面的最上方添加一个高度等于状态栏高度的占位视图,这样页面内容就不会状态栏重叠了。 然后,用户可能需要一个自定义的导航栏组件,包含状态栏占位和自定义的导航内容。这个组件应该在每个页面的顶部使用,确保内容从状态栏下方开始布局。 此外,可能还需要处理不同机型的适配问题,比如iPhone X以后的机型有刘海屏,状态栏高度可能不同。通过wx.getSystemInfo获取的statusBarHeight应该是准确的,可以自动适配不同设备。 另外,引用1中提到使用wx.pageScrollTo来调整页面位置,但在这个问题中可能不需要滚动,而是直接通过布局来调整。不过,如果有输入框需要显示在键盘上方,可能需要结合adjust-position属性和布局调整,但用户当前的问题主要是状态栏重叠,所以暂时可以专注于占位视图的添加。 总结步骤应该是: 1. 在页面的json中设置自定义导航栏。 2. 在页面wxml中添加状态栏占位视图,高度为statusBarHeight。 3. 使用自定义导航栏组件,并确保其高度包括状态栏和导航栏部分。 4. 调整页面内容布局,避免被状态栏遮挡。 还需要注意,自定义组件可能需要全局引入,并在各个页面中正确引用。同时,测试不同设备以确保statusBarHeight的正确获取和布局的适配。 可能遇到的问题包括:状态栏高度获取失败、不同设备兼容性、样式错误导致占位不准确等。需要确保在app.js中正确获取系统信息,并在页面中动态应用这些高度值。 最后,生成相关问题,可能涉及如何动态改变导航栏标题、如何处理键盘弹出时的布局调整,以及自定义导航栏的其他优化方法等。</think>### 微信小程序隐藏导航栏后避免状态栏重叠的解决方案 在微信小程序中,若通过设置`"navigationStyle": "custom"`隐藏原生导航栏,页面内容会从屏幕顶部开始布局,导致状态栏重叠。以下是分步解决方案: --- #### 1. **获取状态栏高度** 通过`wx.getSystemInfo`获取设备状态栏高度,并在全局保存: ```javascript // app.js App({ globalData: { statusBarHeight: 0 }, onLaunch() { wx.getSystemInfo({ success: (res) => { this.globalData.statusBarHeight = res.statusBarHeight; } }); } }); ``` --- #### 2. **在页面顶部添加状态栏占位视图** 在页面布局中预留状态栏高度的空间,使用内联样式动态设置高度: ```html <!-- 页面.wxml --> <view style="height: {{statusBarHeight}}px;"></view> <view>页面内容区域</view> ``` ```javascript // 页面.js const app = getApp(); Page({ data: { statusBarHeight: app.globalData.statusBarHeight } }); ``` --- #### 3. **自定义导航栏组件(可选)** 若需添加自定义导航栏,可创建组件并集成状态栏占位: ```html <!-- components/custom-navbar.wxml --> <view style="height: {{statusBarHeight}}px; background: white;"></view> <view class="custom-nav-content">自定义标题或按钮</view> ``` ```javascript // components/custom-navbar.js const app = getApp(); Component({ properties: { title: String }, data: { statusBarHeight: app.globalData.statusBarHeight } }); ``` --- #### 4. **适配键盘弹出场景** 若页面包含输入框,需设置`adjust-position="{{false}}"`防止键盘顶起页面,并通过`wx.pageScrollTo`手动调整位置: ```html <input adjust-position="{{false}}" bindfocus="onInputFocus" /> ``` ```javascript Page({ onInputFocus(e) { wx.pageScrollTo({ scrollTop: e.detail.height, duration: 300 }); } }); ``` --- ### 关键点总结 - **动态计算高度**:通过`statusBarHeight`确保布局适配不同机型[^2]。 - **占位视图**:在页面顶部添加状态栏等高的空白区域[^3]。 - **键盘交互优化**:结合`adjust-position`和滚动控制提升输入体验[^1]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值