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的层级关系,会发现UIWindow下只有presented VC.
UIModalPresentationPageSheet
在常规型设备(大屏手机,例如plus系列以及iPad系列)的水平方向,presented VC的高为当前屏幕的高度,宽为该设备竖直方向屏幕的宽度,其余部分用透明背景做填充。对于紧凑型设备(小屏手机)的水平方向及所有设备的竖直方向,其显示效果与UIModalPresentationFullScreen相同。
UIModalPresentationFormSheet
在常规型设备的水平方向,presented VC的宽高均小于屏幕尺寸,其余部分用透明背景填充。对于紧凑型设备的水平方向及所有设备的竖直方向,其显示效果与UIModalPresentationFullScreen相同
UIModalPresentationCurrentContext
使用这种方式present VC时,presented VC的宽高取决于presentation context的宽高,并且UIKit会寻找属性definesPresentationContext为YES的VC作为presentation context,具体的寻找方式会在下文中给出 。当此次presentation完成之后,presentation context及其子VC都将被暂时移出当前的UI栈。
UIModalPresentationCustom
自定义模式,需要实现UIViewControllerTransitioningDelegate的相关方法,并将presented VC的transitioningDelegate 设置为实现了UIViewControllerTransitioningDelegate协议的对象。
UIModalPresentationOverFullScreen
与UIModalPresentationFullScreen的唯一区别在于,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下使用这种方式将会触发异常。
UIModalPresentationBlurOverFullScreen
presentation完成之后,如果presented VC的背景有透明部分,会看到presented VC下面的VC会变得模糊,其他与UIModalPresentationOverFullScreen模式没有区别。
present VC是通过UIViewController的
presentViewController: animated:completion:
函数实现的,在探讨他们之间的层级关系之前,我们首先要理解一个概念,就是presentation context。
- ### presentation context
presentation context是指为本次present提供上下文环境的类,需要指出的是,presenting
VC通常并不是presentation context,Apple官方文档对于presentation context的选择是这样介绍的: