H5頁面WebView在iPhoneX的適配

1. iPhoneX的介紹

屏幕尺寸 我們熟知的iPhone系列開發(fā)尺寸概要如下:

△ iPhone各機型的開發(fā)尺寸

轉(zhuǎn)化成我們熟知的像素尺寸:

△ 每個機型的多維度尺寸

倍圖其實就是像素尺寸和開發(fā)尺寸的倍率關(guān)系放接,但這只是外在的表現(xiàn)。倍圖核心的影響因素在于PPI(DPI)帚湘,了解屏幕密度與各尺寸的關(guān)系有助于我們深度理解倍率的概念:《基礎(chǔ)知識學(xué)起來军洼!為設(shè)計師量身打造的DPI指南》

iPhone8在本次升級中巩螃,屏幕尺寸和分辨率都遺傳了iPhone6以后的優(yōu)良傳統(tǒng);然而iPhone X 無論是在屏幕尺寸匕争、分辨率避乏、甚至是形狀上都發(fā)生了較大的改變,下面以iPhone 8作為參照物甘桑,看看到底iPhone X的適配我們要怎么考慮拍皮。

我們看看iPhone X尺寸上的變化:

2. iPhoneX的適配---安全區(qū)域(safe area)

蘋果對于 iPhone X 的設(shè)計布局意見如下:

核心內(nèi)容應(yīng)該處于 Safe area 確保不會被設(shè)備圓角(corners)歹叮,傳感器外殼(sensor housing,齊劉海) 以及底部的 Home Indicator 遮擋铆帽。也就是說 我們設(shè)計顯示的內(nèi)容應(yīng)該盡可能的在安全區(qū)域內(nèi)咆耿;

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

對于iOS原生來說可以控制webView來適配安全區(qū)域

if (@available(ios 11.0,*)) 
{
 UIScrollView.appearance.contentInsetAdjustmentBehavior = 
                                    UIScrollViewContentInsetAdjustmentNever;
 }

不知道大家有沒有在appdelegate里面寫過這句話,這句話的意思就是不讓scroview的子類自動適配安全區(qū)域爹橱。當(dāng)你不寫這句話你會發(fā)現(xiàn)萨螺,你的tableview拉到底之后頂上來了,正好是底部橫杠的高度愧驱,但是當(dāng)你寫了之后就會發(fā)現(xiàn)這個功能能沒有了慰技。

參考一下使用:

  1. 在iphoneX的時候增加webview高度34,這個方法太局限组砚,不提倡
  2. 在iphoneX的時候改變webview.scroview的高度吻商,這個方法我也不推薦,我沒用過糟红。
  3. 在iphoneX的時候設(shè)置webview.scroview.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;這個方法目前我感覺是最靠譜艾帐,直接讓webview放棄適配安全區(qū)域,當(dāng)然寫上了這個改化,在webview滑到底部的時候就不會頂上來了掩蛤。
3.1 PhoneX的適配,在iOS 11中采用了viewport-fit的meta標(biāo)簽作為適配方案陈肛;

viewport-fit的默認值是auto揍鸟。viewport-fit取值如下:
auto 默認:viewprot-fit:contain;頁面內(nèi)容顯示在safe area內(nèi)
cover viewport-fit:cover,頁面內(nèi)容充滿屏幕

viewport-fit meta標(biāo)簽設(shè)置(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()函數(shù) 與safe-area-inset-top & safe-area-inset-left & safe-area-inset-right & safe-area-inset-bottom的介紹

如上圖所示 在iOS 11中的WebKit包含了一個新的CSS函數(shù)constant(),以及一組四個預(yù)定義的常量:safe-area-inset-left, safe-area-inset-right, safe-area-inset-top和 safe-area-inset-bottom句旱。當(dāng)合并一起使用時阳藻,允許樣式引用每個方面的安全區(qū)域的大小。

  • 當(dāng)我們設(shè)置viewport-fit:contain,也就是默認的時候時;設(shè)置safe-area-inset-left, safe-area-inset-right, safe-area-inset-top和 safe-area-inset-bottom等參數(shù)時不起作用的谈撒。

  • 當(dāng)我們設(shè)置viewport-fit:cover時:設(shè)置如下

body {
    padding-top: constant(safe-area-inset-top);   //為導(dǎo)航欄+狀態(tài)欄的高度 88px            
    padding-left: constant(safe-area-inset-left);   //如果未豎屏?xí)r為0                
    padding-right: constant(safe-area-inset-right); //如果未豎屏?xí)r為0                
    padding-bottom: constant(safe-area-inset-bottom);//為底下圓弧的高度 34px       
}
4. iPhoneX的適配---高度統(tǒng)計

viewport-fit:cover + 導(dǎo)航欄

5. iPhoneX的適配---媒體查詢

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

@media only screen and (width: 375px) and (height: 690px){
    body {
        background: blue;
    }
}
6. iphoneX viewport-fit 問題總結(jié)
1. 關(guān)于iphoneX 頁面使用了漸變色時;如果viewport-fit:cover;

1.1 在設(shè)置了背景色單色和漸變色的區(qū)別啃匿,如果是單色時會填充整個屏幕蛔外,如果設(shè)置了漸變色 那么只會更加子元素的高度去渲染;而且頁面的高度只有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設(shè)置如下

<!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時只會顯示在安全區(qū)域內(nèi)裆悄;

<!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時只會顯示在安全區(qū)域內(nèi);

* {
    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 關(guān)于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" οnclick="showLoading()">彈框加載</button>
   </section>
   <script type="text/javascript" src="alertView.js"></script>
   <script>
       function showLoading() {
           UIAlertView.show({
               type:"input",
               title:"溫馨提示",              //標(biāo)題
               content:"VIP會員即將到期",     //獲取新的
               isKnow:false
           });
           var xx = new UIAlertView();
          console.log(xx);
       }
   </script>
</body>
</html>

參考:https://www.cnblogs.com/lolDragon/p/7795174.html
http://www.cocoachina.com/ios/20171222/21632.html

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末光稼,一起剝皮案震驚了整個濱河市或南,隨后出現(xiàn)的幾起案子孩等,更是在濱河造成了極大的恐慌,老刑警劉巖采够,帶你破解...
    沈念sama閱讀 217,734評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件肄方,死亡現(xiàn)場離奇詭異,居然都是意外死亡吁恍,警方通過查閱死者的電腦和手機扒秸,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,931評論 3 394
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人乐纸,你說我怎么就攤上這事许赃。” “怎么了母剥?”我有些...
    開封第一講書人閱讀 164,133評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我感局,道長,這世上最難降的妖魔是什么暂衡? 我笑而不...
    開封第一講書人閱讀 58,532評論 1 293
  • 正文 為了忘掉前任询微,我火速辦了婚禮,結(jié)果婚禮上狂巢,老公的妹妹穿的比我還像新娘撑毛。我一直安慰自己,他們只是感情好唧领,可當(dāng)我...
    茶點故事閱讀 67,585評論 6 392
  • 文/花漫 我一把揭開白布藻雌。 她就那樣靜靜地躺著,像睡著了一般斩个。 火紅的嫁衣襯著肌膚如雪胯杭。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,462評論 1 302
  • 那天受啥,我揣著相機與錄音做个,去河邊找鬼。 笑死滚局,一個胖子當(dāng)著我的面吹牛居暖,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播核畴,決...
    沈念sama閱讀 40,262評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼膝但,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了谤草?” 一聲冷哼從身側(cè)響起跟束,我...
    開封第一講書人閱讀 39,153評論 0 276
  • 序言:老撾萬榮一對情侶失蹤莺奸,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后冀宴,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體灭贷,經(jīng)...
    沈念sama閱讀 45,587評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,792評論 3 336
  • 正文 我和宋清朗相戀三年略贮,在試婚紗的時候發(fā)現(xiàn)自己被綠了甚疟。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,919評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡逃延,死狀恐怖览妖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情揽祥,我是刑警寧澤讽膏,帶...
    沈念sama閱讀 35,635評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站拄丰,受9級特大地震影響府树,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜料按,卻給世界環(huán)境...
    茶點故事閱讀 41,237評論 3 329
  • 文/蒙蒙 一奄侠、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧载矿,春花似錦垄潮、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,855評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至馁筐,卻和暖如春涂召,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背敏沉。 一陣腳步聲響...
    開封第一講書人閱讀 32,983評論 1 269
  • 我被黑心中介騙來泰國打工果正, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人盟迟。 一個月前我還...
    沈念sama閱讀 48,048評論 3 370
  • 正文 我出身青樓秋泳,卻偏偏與公主長得像,于是被迫代替她去往敵國和親攒菠。 傳聞我的和親對象是個殘疾皇子迫皱,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,864評論 2 354

推薦閱讀更多精彩內(nèi)容