iOS中爲網站添加圖標到主屏幕以及增加啓動畫面

雖然沒有能力開發Native App,但還是可以利用iOS中Safari瀏覽器的特性小小的折騰一下,做一個僞Web App滿足下小小的虛榮心的。

既然是在iOS中的Safari折騰的,那麼代碼中利用到的也基本上都是Safari的私有屬性。

添加圖標到主屏幕是Web App的第一步:

<link rel="apple-touch-icon-precomposed" sizes="57x57" href="icon-57.png">
<link rel="apple-touch-icon-precomposed" sizes="72x72" href="icon-72.png">
<link rel="apple-touch-icon-precomposed" sizes="114x114" href="icon-114.png">
<link rel="apple-touch-icon-precomposed" sizes="144x144" href="icon-144.png">

添加圖標到屏幕裏的有兩種屬性值apple-touch-icon和apple-touch-icon-precomposed,區別就在於是否會應用iOS中自動給圖標添加的那層高光。

由於iPhone以及iPad都有兩種分辨率的設備存在,圖標的尺寸就需要做4個:144×144(iPad Retina)、72×72(iPad)、114×114(iPhone Retina)、57×57(iPhone)。

可以使用sizes尺寸來告訴設備該調用哪個圖標。

有了圖標還不夠像,還需要加上啓動畫面:

<link rel="apple-touch-startup-image" sizes="2048x1496" href="icon-2048x1496.png" media="screen and (min-device-width: 1025px) and (max-device-width: 2048px) and (orientation:landscape) and (-webkit-min-device-pixel-ratio: 2)">
<link rel="apple-touch-startup-image" sizes="1536x2008" href="icon-1536x2008.png" media="screen and (min-device-width: 1025px) and (max-device-width: 2048px) and (orientation:portrait) and (-webkit-min-device-pixel-ratio: 2)">

<link rel="apple-touch-startup-image" sizes="1024x748" href="icon-1024x748.png" media="screen and (min-device-width: 481px) and (max-device-width: 1024px) and (orientation:landscape)">
<link rel="apple-touch-startup-image" sizes="768x1004" href="icon-768x1004.png" media="screen and (min-device-width: 481px) and (max-device-width: 1024px) and (orientation:portrait)">
<link rel="apple-touch-startup-image" sizes="640x920" href="icon-640x920.png" media="screen and (max-device-width: 480px) and (-webkit-min-device-pixel-ratio: 2)">
<link rel="apple-touch-startup-image" sizes="320x460" href="icon-320x460.png" media="screen and (max-device-width: 320)">

apple-touch-startup-image是用來標示啓動畫面的,但它不像apple-touch-icon可以指定sizes來告訴設備該使用哪個圖片(也有一種說法是在iOS5中已經支持sizes識別了,但沒有測試成功),所以只能通過media裏的設備分辨率的判斷值來識別,而iPhone Retina的分辨率值界於取值之間,所以需要通過webkit的私有屬性-webkit-min-device-pixel-ratio:2來鑑別像素密度以進行識別。

啓動畫面的圖片尺寸並非完全等於設備的尺寸,比如iPad2的尺寸是1024×768,但它的啓動畫面尺寸橫向是1024×748,豎向尺寸是768×1004,因爲需要減去系統狀欄的高度20px,而使用的Retina屏幕的iPhone4以及iPad3則需要減去狀態欄的高度40px。

Web App運行起來要像Native App,那麼就要去掉Safari的一些默認控件,比如地址欄、狀態欄之類的。

<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="format-detection" content="telephone=no">
<meta name="viewport" content="width=device-width,initial-scale=1, minimum-scale=1.0, maximum-scale=1, user-scalable=no">

apple-mobile-web-app-capable是用來定義應用全屏展示的;在定義了apple-mobile-web-app-capable的前提下,設置狀態欄的屬性值apple-mobile-web-app-status-bar-style纔有效;format-detection的值用於啓用或禁用自動檢測在網頁中可能出現的電話號碼;

viewport並非Safari的私有屬性,width用於指定寬度,initial-scale指定初始化的縮略比例,minimum-scale指定縮小的比例,而maximum-scale則是放大的比例,當然這些縮放都取決於user-scalable——決定用戶是否能縮放頁面。

更正:

雖然New iPad採用了Retina屏幕,但實際上物理分辨率並沒有變,還是1024*768的,所以以上代碼中的New iPad的啓動畫面代碼尺寸有誤,應該是

<link rel="apple-touch-startup-image" sizes="2048x1496" href="icon-2048x1496.png" media="screen and (min-device-width:481px) and (max-device-width:1024px) and (orientation:landscape) and (-webkit-min-device-pixel-ratio: 2)">
<link rel="apple-touch-startup-image" sizes="1536x2008" href="icon-1536x2008.png" media="screen and (min-device-width:481px) and (max-device-width:1024px) and (orientation:portrait) and (-webkit-min-device-pixel-ratio: 2)">

發佈了166 篇原創文章 · 獲贊 7 · 訪問量 37萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章