iOS8 Size Classes的理解與使用

來源:互聯網
上載者:User

標籤:http   io   ar   os   使用   sp   for   strong   div   

Size Classes是什麼

iOS 8在應用介面的可視化設計上添加了一個新的特性-Size Classes,對於任何裝置來說,介面的寬度和高度都只分為兩種描述:正常緊湊。這樣開發人員便可以無視裝置具體的尺寸,而是對這兩類和它們的組合進行適配。這樣不論在設計時還是代碼上,我們都可以不再受限於具體的尺寸,而是變成遵循尺寸的視覺感官來進行適配。在Xcode中的具體體現如:

但是我們看到圖中的寬度和高度都是Any,Any是什麼意思呢?如果weight設為Anyheight設定為Regular,那麼在該狀態下的介面元素在只要heightRegular,無論weightRegular還是Compact的狀態中都會存在。這種關係應該叫做繼承關係,具體的四種介面描述與可繼承的介面描述如下:

  • w:Compact h:Compact 繼承 (w:Any h:Compact , w:Compact h:Any , w:Any h:Any)
  • w:Regular h:Compact 繼承 (w:Any h:Compact , w:Regular h:Any , w:Any h:Any)
  • w:Compact h:Regular 繼承 (w:Any h:Regular , w:Compact h:Any , w:Any h:Any)
  • w:Regular h:Regular 繼承 (w:Any h:Regular , w:Regular h:Any , w:Any h:Any)

我們知道了iOS 8下面裝置介面可以描述為4種,但是這麼多裝置(iPhone4S,iPhone5/5s,iPhone6,iPhone6 Plus,iPad,Apple Watch)具體對應什麼描述呢?經過查看官方文檔和具體實踐得知具體對應關係如下:

  • iPhone4S,iPhone5/5s,iPhone6
    • 豎屏:(w:Compact h:Regular)
    • 橫屏:(w:Compact h:Compact)
  • iPhone6 Plus
    • 豎屏:(w:Compact h:Regular)
    • 橫屏:(w:Regular h:Compact)
  • iPad
    • 豎屏:(w:Regular h:Regular)
    • 橫屏:(w:Regular h:Regular)
  • Apple Watch(猜測)
    • 豎屏:(w:Compact h:Compact)
    • 橫屏:(w:Compact h:Compact)
Size Classes手寫代碼

為了表徵Size Classes,Apple在iOS8中引入了一個新的類,UITraitCollection。這個類封裝了像水平和豎直方向的Size Class等資訊。iOS8的UIKit中大多數UI的基礎類(包括UIScreen,UIWindow,UIViewController和UIView)都實現了UITraitEnvironment這個介面,通過其中的traitCollection這個屬性,我們可以拿到對應的UITraitCollection對象,從而得知當前的Size Class,並進一步確定介面的布局。和UIKit中的響應者鏈正好相反,traitCollection將會在view hierarchy中自上而下地進行傳遞。對於沒有指定traitCollection的UI組件,將使用其父節點的traitCollection。這在布局包含childViewController的介面的時候會相當有用。在UITraitEnvironment這個介面中另一個非常有用的是-traitCollectionDidChange:。在traitCollection發生變化時,這個方法將被調用。在實際操作時,我們往往會在ViewController中重寫-traitCollectionDidChange:或者-willTransitionToTraitCollection:withTransitionCoordinator:方法(對於ViewController來說的話,後者也許是更好的選擇,因為提供了轉場上下文方便進行動畫;但是對於普通的View來說就只有前面一個方法了),然後在其中對當前的traitCollection進行判斷,並進行重新布局以及動畫。代碼看起來大概會是這個樣子:

123456789101112131415
- (void)willTransitionToTraitCollection:(UITraitCollection *)newCollection               withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinator{    [super willTransitionToTraitCollection:newCollection                  withTransitionCoordinator:coordinator];    [coordinator animateAlongsideTransition:^(id <UIViewControllerTransitionCoordinatorContext> context)     {        if (newCollection.verticalSizeClass == UIUserInterfaceSizeClassCompact) {            //To Do: modify something for compact vertical size        } else {            //To Do: modify something for other vertical size        }        [self.view setNeedsLayout];    } completion:nil];}

在兩個To Do處,我們要手寫代碼針對不同的狀態做調整。

Size Classes與Interface Builder

Xcode6中Interface BuilderSize Class有了很強大的支援,xib中可以開啟Size Classes如:

在不同的Size Classes描述下,介面元素可以選擇安裝還是不安裝,具體操作

Size Classes與Image Asset

Xcode6中Image Asset也支援了Size Class,也就是說,我們可以對不同的Size Class指定不同的圖片了。在Image Asset的編輯面板中選擇某張圖片,Inspector裡現在多了一個WidthHeight的組合,添加我們需要對應的Size Class,然後把合適的圖拖上去,這樣在運行時SDK就將從中挑選對應的Size的圖進行替換了。支援Size ClassImage Asset編輯效果如下:

 

iOS8 Size Classes的理解與使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.