UIPresentationController是提供高级视图切换的类。它让管理present ViewController的过程变得简单。app
先讲一些presentation基础知识,在iPad的设置页面,能够经过popOver弹出一个UIViewController,这个弹出的,能够和用户交互的Controller叫作PresentedViewController,然后面那个被部分遮挡的UIViewController叫作PresentingViewController,而在UIPresentationController中,PresentedViewController是presentation的content,而PresentingViewController叫作Chrome。以下图:ide



全部的UIViewController的presentation都是由UIPresentationController管理的。在UIPresentationController中能够定义content和chrome的动画,能够根据大小的变化来改变content大小,能够根据系统的不一样,来改变展现方式,UIPresentationController也是可复用的,能够很容易的拿到其余的UIViewController中去使用。post
UIPopoverPresentationController测试
它在iOS8中替代了UIPopoverController,它在功能上与旧的controller彻底等同,而且新增了一些内置的适配特性,能够自动适配iPad与iPhone。如下是新版与旧版接口的比较:动画
UIPopoverController使用方法:编码

咱们先声明了一个UIViewController做为content controller,使用它初始化了一个UIPopoverController,而后调用presentPopover方法来展现它。这是在iPad上的用法。若是要建立一个在iPad与iPhone上通用的方法,那么须要如下的代码:spa

咱们须要去判断设备是否为iPad,若为iPad,那么建立一个UIPopoverController并展现它,若不是iPad,那么就须要调用常规的presentViewController方法来展现一个UIViewController。3d
然而咱们若是使用UIPopoverPresentationController,那么就再也不须要判断设备:blog

将UIViewController的modalPresentationStyle设置成UIModalPresentationPopover,这个值用来实现popover效果,而且各个平台自动适应。第二行中,经过popoverPresentationController属性来获取它的popoverPresentationController,而不是建立一个新的。而后设置它的一些界面属性,最后调用presentViewController方法来显示这个controller。这样就能够在iPad与iPhone显示自动适应的popover效果了,以下图所示:


可见,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间,自动适应使得编码更简洁。