iOS8新特性 UIPresentationController(一)

來源:互聯網
上載者:User

標籤:

UIPresentationController(一)popover、alert、action sheet與search的新特性
  • 劉冰 發表於測試?看我的
劉冰

 

2014年07月02日 發表收錄了本文的主題測試?看我的

一枚測試人員很小,這個世界很大,讓我們的分享無限!學點有用的:)

+ 訂閱更多收錄本文的主題

UIPresentationController是提供進階視圖切換的類。它讓管理present ViewController的過程變得簡單。

先講一些presentation基礎知識,在iPad的設定頁面,可以通過popOver彈出一個UIViewController,這個彈出的,可以和使用者互動的Controller叫做PresentedViewController,而後面那個被部分遮擋的UIViewController叫做PresentingViewController,而在UIPresentationController中,PresentedViewController是presentation的content,而PresentingViewController叫做Chrome。如:

PresentedViewController和PresentingViewControllerContent



Chrome


所有的UIViewController的presentation都是由UIPresentationController管理的。在UIPresentationController中可以定義content和chrome的動畫,可以根據大小的變化來改變content大小,可以根據系統的不同,來改變展示方式,UIPresentationController也是可複用的,可以很容易的拿到其他的UIViewController中去使用。

UIPopoverPresentationController

它在iOS8中替代了UIPopoverController,它在功能上與舊的controller完全等同,並且新增了一些內建的適配特性,可以自動適配iPad與iPhone。以下是新版與舊版介面的比較:

UIPopoverController使用方法:

我們先聲明了一個UIViewController作為content controller,使用它初始化了一個UIPopoverController,然後調用presentPopover方法來展示它。這是在iPad上的用法。如果要建立一個在iPad與iPhone上通用的方法,那麼需要以下的代碼:

我們需要去判斷裝置是否為iPad,若為iPad,那麼建立一個UIPopoverController並展示它,若不是iPad,那麼就需要調用常規的presentViewController方法來展示一個UIViewController。

然而我們如果使用UIPopoverPresentationController,那麼就不再需要判斷裝置:

將UIViewController的modalPresentationStyle設定成UIModalPresentationPopover,這個值用來實現popover效果,並且各個平台自動適應。第二行中,通過popoverPresentationController屬性來擷取它的popoverPresentationController,而不是建立一個新的。然後設定它的一些介面屬性,最後調用presentViewController方法來顯示這個controller。這樣就可以在iPad與iPhone顯示自動適應的popover效果了,如所示:

iPad效果iPhone效果

可見,iPhone上,只是作為一個普通的UITableViewController展示出來。

iPhone上的自適應是在delegate中實現的:

在第一個方法中,指定了UIModelPresentationFullScreen的樣式來展示controller。第二個方法中,我們將presentedViewController使用UINavigationController封裝起來,使得可以在選中某項之後,通過navigationController提供的一些方法來展示內容,或者後退。

UIAlertView與UIActionSheet

UIAlertView與UIActionSheet都是繼承自UIView的,但是它們在實現時,卻用了一些UIViewController的方式,並且它的介面比較舊,採用的是delegate方式。在iOS8中,新增了UIAlertController,它可以同時實現Alert和Action Sheets,並且介面採用block方式,它將在應用的同一個window中展示,而不是之前的新window中展示,它還具有和之前的popover controller相同的實現方式,通過presentViewController來展示。下面是新的UIAlertController的用法:

首先建立一個UIAlertController,之後通過addAction方法來增加一些action,而UIAlertAction使用block來設定按鈕的點擊處理方法。最後,通過調用presentViewController來展示UIAlertController。

UISearchDisplayController

search在iOS中包含兩部分:UISearchBar與UISearchDisplayController。它們都是很古老的介面,功能與樣式都不能滿足現狀的應用,UISearchDisplayController的配置項很少,它只能在tableView中顯示結果,而不能在collectionView或者mapView中顯示。它的present過程只是通過addSubview來將view作為一個子view添加上去。而如果它的父view是一個scrollView,那麼手勢處理就會有衝突。在iOS8中,引入了UISearchController,它允許presentingController是任意類型的,不一定是全螢幕的,search bar animation可以自訂,並且可以在不同平台上適配。

在之前的介面中,我們使用UISearchDisplayController來實現search功能:

首先初始化一個UISearchBar,再用它初始化一個UISearchDisplayController,指定controller的searchResuleDataSource與searchResultDelegate,之後,將searchBar作為當前view中的tableView的tableHeaderView來顯示。而在iOS8中,實現方式是這樣的:

首先初始化一個自訂的resultsController,可以是一個collectionView,也可以是一些其他的自訂樣式。之後使用這個resultsController建立一個UISearchController,指定searchController的searchResultsUpdater為resultsController,之後將searchBar加到tableView的tableHeaderView上,最後,設定當前Controller的definesPresentationContext為YES,表示UISearchController在present時,可以覆蓋當前controller。

經過這樣的修改,所有的都變成了Controller,不再有UIView組件,也不再需要通過建立新window來展示UIView,更加容易控制。UIPresentationController為Content與Chrome提供了一個很好的抽象,並且在iPad與iPhone間,自動適應使得編碼更簡潔。

iOS8新特性 UIPresentationController(一)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.