原文
presenting view controller & presented view controller
當VCA模態(tài)的彈出了VCB绿满,那么VCA就是presenting view controller评凝,VCB就是presented view controller蟀苛,具體在代碼中體現(xiàn)如下:
[VCA presentViewController:VCB animated:YES completion:nil];
container view controller
container view controller 指的是VC的容器類,通過container view controller侈玄,我們可以很方便的管理子VC婉刀,實現(xiàn)VC之間的跳轉等,iOS中container view controller包括g UINavigationController, UITabbarViewController,UISplitViewController, 以及 UIPageViewController.
presented VC 的modalPresentationStyle屬性決定了此次presentation的行為方式及UIKit尋找presentation context的方法拗馒,iOS提供了以下幾種常用的presentation style
- UIModalPresentationFullScreen
UIKit默認的presentation style路星。 使用這種模式時溯街,presented VC的寬高與屏幕相同诱桂,并且UIKit會直接使用rootViewController做為presentation context洋丐,在此次presentation完成之后,UIKit會將presentation context及其子VC都移出UI棧挥等,這時候觀察VC的層級關系友绝,會發(fā)現(xiàn)UIWindow下只有presented VC.
- UIModalPresentationPageSheet
在常規(guī)型設備(大屏手機,例如plus系列以及iPad系列)的水平方向肝劲,presented VC的高為當前屏幕的高度迁客,寬為該設備豎直方向屏幕的寬度,其余部分用透明背景做填充辞槐。對于緊湊型設備(小屏手機)的水平方向及所有設備的豎直方向掷漱,其顯示效果與UIModalPresentationFullScreen相同。
- UIModalPresentationFormSheet
在常規(guī)型設備的水平方向榄檬,presented VC的寬高均小于屏幕尺寸卜范,其余部分用透明背景填充。對于緊湊型設備的水平方向及所有設備的豎直方向鹿榜,其顯示效果與UIModalPresentationFullScreen相同
- UIModalPresentationCurrentContext
使用這種方式present VC時海雪,presented VC的寬高取決于presentation context的寬高,并且UIKit會尋找屬性definesPresentationContext為YES的VC作為presentation context舱殿,具體的尋找方式會在下文中給出 奥裸。當此次presentation完成之后,presentation context及其子VC都將被暫時移出當前的UI棧沪袭。
- UIModalPresentationCustom
自定義模式湾宙,需要實現(xiàn)UIViewControllerTransitioningDelegate的相關方法,并將presented VC的transitioningDelegate 設置為實現(xiàn)了UIViewControllerTransitioningDelegate協(xié)議的對象冈绊。
- UIModalPresentationOverFullScreen
與UIModalPresentationFullScreen的唯一區(qū)別在于创倔,UIWindow下除了presented VC,還有其他正常的VC層級關系焚碌。也就是說該模式下畦攘,UIKit以rootViewController為presentation context,但presentation完成之后不會講rootViewController移出當前的UI棧十电。
- UIModalPresentationOverCurrentContext
尋找presentation context的方式與UIModalPresentationCurrentContext相同知押,所不同的是presentation完成之后,不會將context及其子VC移出當前UI棧鹃骂。但是台盯,這種方式只適用于transition style為UIModalTransitionStyleCoverVertical的情況(UIKit默認就是這種transition style)。其他transition style下使用這種方式將會觸發(fā)異常畏线。
- UIModalPresentationBlurOverFullScreen
presentation完成之后静盅,如果presented VC的背景有透明部分,會看到presented VC下面的VC會變得模糊寝殴,其他與UIModalPresentationOverFullScreen模式沒有區(qū)別蒿叠。
present VC是通過UIViewController的presentViewController: animated:completion:
函數(shù)實現(xiàn)的明垢,在探討他們之間的層級關系之前,我們首先要理解一個概念市咽,就是presentation context痊银。
presentation context
presentation context是指為本次present提供上下文環(huán)境的類,需要指出的是施绎,presenting VC通常并不是presentation context溯革,Apple官方文檔對于presentation context的選擇是這樣介紹的:
從上面的介紹可以看出,當我們需要present VC的時候谷醉,除非我們指定了context致稀,否則UIKit會優(yōu)先選擇presenting VC所屬的容器類做為presentation context,如果沒有容器類俱尼,那么會選擇rootViewController豺裆。但是,UIKit搜索context的方式還與presented VC的modalPresentationStyle屬性有關号显,當modalPresentationStyle為UIModalPresentationFullScreen臭猜、UIModalPresentationOverFullScreen等模式時,UIKit會直接選擇rootViewController做為context押蚤。當modalPresentationStyle為UIModalPresentationOverCurrentContext蔑歌、UIModalPresentationCurrentContext模式時,UIKit搜索context的方式如下:
When you present a view controller, UIKit looks for a view controller that provides a suitable context for the presentation. In many cases, UIKit chooses the nearest container view controller but it might also choose the window’s root view controller. In some cases, you can also tell UIKit which view controller defines the presentation context and should handle the presentation.
UIModalPresentationOverCurrentContext揽碘、UIModalPresentationCurrentContext模式下次屠,一個VC能否成為presentation context 是由VC的definesPresentationContext屬性決定的,這是一個BOOL值雳刺,默認UIViewController的definesPresentationContext屬性值是NO劫灶,而 container view controller的definesPresentationContext默認值是YES,這也是上文中掖桦,UIKit總是將container view controller做為presentation context的原因本昏。如果我們想指定presenting VC做為context,只需要在presenting VC的viewDidLoad方法里添加如下代碼即可
UIKit搜索presentation context的順序為:
- presenting VC
- presenting VC 的父VC
- presenting VC 所屬的container VC
- rootViewController
還有另外一種特殊情況枪汪,當我們在一個presented VC上再present一個VC時涌穆,UIKit會直接將這個presented VC做為presentation context。