详解iOS的presentViewController(转)
一、用途和相关概念
iOS中显示ViewController的方式有两种push和modal,modal也叫模态,模态显示VC是iOS的重要特性之一,其主要用于有以下场景:
- 收集用户输入信息
- 临时呈现一些内容
- 临时改变工作模式
- 相应设备方向变化(用于针对不同方向分别是想两个ViewController的情况)
- 显示一个新的view层级
这些场景都会暂时中断APP的正常执行流程,主要作用是收集信息以及显示一些重要的提示等。
presenting view controller & presented view controller
当VCA模态的弹出了VCB,那么VCA就是presenting view controller,VCB就是presented view controller,具体在代码中体现如下:
[VCA presentViewController:VCB animated:YES completion:nil];
container view controller
container view controller 指的是VC的容器类,通过container view controller,我们可以很方便的管理子VC,实现VC之间的跳转等,iOS中container view controller包括 UINavigationController, UISplitViewController, 以及 UIPageViewController.
二、ModalPresentationStyle & Presentation Context
ModalPresentationStyle
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的选择是这样介绍的:
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.
从上面的介绍可以看出,当我们需要present VC的时候,除非我们指定了context,否则UIKit会优先选择presenting VC所属的容器类做为presentation context,如果没有容器类,那么会选择rootViewController。但是,UIKit搜索context的方式还与presented VC的modalPresentationStyle属性有关,当modalPresentationStyle为UIModalPresentationFullScreen、UIModalPresentationOverFullScreen等模式时,UIKit会直接选择rootViewController做为context。当modalPresentationStyle为UIModalPresentationOverCurrentContext、UIModalPresentationCurrentContext模式时,UIKit搜索context的方式如下:
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方法里添加如下代码即可:
self.definesPresentationContext = YES
UIKit搜索presentation context的顺序为:
1. presenting VC
2. presenting VC 的父 VC
3. presenting VC 所属的 container VC
4. rootViewController
还有另外一种特殊情况,当我们在一个presented VC上再present一个VC时,UIKit会直接将这个presented VC做为presentation context。
三、presenting VC 与presented VC 之间的层级关系
在iOS 中,presented VC 总是与 presentation context 处于同一层级,而与presenting VC所在的层级无关,且同一个presentation context同时只能有一个presented VC。
下面我们通过代码来验证这个结论。首先新建一个APP,各VC之间的层级关系如

在SecondContentVC中present FirstPresentVC,代码如下:
FirstPresentVC *vc = [[FirstPresentVC alloc] init];
vc.modalPresentationStyle = UIModalPresentationOverCurrentContext;
[self presentViewController:vc animated:YES completion:nil];
为了更好的观察各VC的层级关系,我们将presented VC 的modalPresentationStyle属性设置为UIModalPresentationOverCurrentContext,这。然后我们再看各VC之间的层级关系:

可以看到FirstPresentVC 与 SecondContentVC的navigationViewController处在同一层级,说明这时的presentation context是navigationViewController。下面我们在FirstContentVC的viewDidLoad方法里添加如下代码:
self.definesPresentationContext = YES;
弹出FirstPresentVC后再看VC之间的层级关系:

可以看到,FirstPresentVC 与 FirstContentVC 处在同一层级,说明此时的 presentation context 为 FirstContentVC.
下面我们将SecondContentVC的definesPresentationContext属性设为YES,然后观察弹出FirstPresentVC之后的层级关系:
可以看到,此时的presentation context为SecondContentVC。这个实验也验证了UIKit 搜索presentation context的顺序。
四、ModalTransitionStyle
modalTransitionStyle可以设置presentation的转场动画,官方提供了几种不同的转场动画,默认是UIModalTransitionStyleCoverVertical。如果想要使用别的style,只需要设置presented VC的modalTransitionStyle属性即可。其余三种包括UIModalTransitionStyleFlipHorizontal、UIModalTransitionStyleCrossDissolve、UIModalTransitionStylePartialCurl.具体每种style的表现形式参考Demo.
「tianweitao」原创文章,
原文链接:https://blog.csdn.net/tianweitao/article/details/80314598
详解iOS的presentViewController(转)的更多相关文章
- 深拷贝与浅拷贝(mutableCopy与Copy)详解 iOS
深拷贝与浅拷贝(mutableCopy与Copy)详解 iOS ios中并不是所有的对象都支持copy,mutableCopy,遵守NSCopying 协议的类可以发送copy消息,遵守NSMutab ...
- 详解iOS开发之自定义View
iOS开发之自定义View是本文要将介绍的内容,iOS SDK中的View是UIView,我们可以很方便的自定义一个View.创建一个 Window-based Application程序,在其中添加 ...
- 【转】详解iOS应用程序内使用IAP/StoreKit付费、沙盒(SandBox)测试、创建测试账号流程
http://blog.csdn.net/xiaominghimi/article/details/6937097 //——2012-12-11日更新 获取"产品付费数量等于0这个问题& ...
- 【iOS开发必收藏】详解iOS应用程序内使用IAP/StoreKit付费、沙盒(SandBox)测试、创建测试账号流程!【2012-12-11日更新获取”产品付费数量等于0的问题”】
转的别人的 看到很多童鞋问到,为什么每次都返回数量等于0?? 其实有童鞋已经找到原因了,原因是你在 ItunesConnect 里的 “Contracts, Tax, and Banking”没有完成 ...
- 详解iOS应用程序内使用IAP/StoreKit付费、沙盒(SandBox)测试、创建测试账号流程
Himi 原创, 欢迎转载,转载请在明显处注明! 谢谢. 原文地址:http://blog.csdn.net/xiaominghimi/article/details/6937097 //——201 ...
- 详解 IOS 7.1 程序启动原理
程序都是从Main方法入口的 IOS 也不例外 int main(int argc,char * argv[]) { @autoreleasepool { return UIApplicationMa ...
- 详解 iOS 上机题!附个人见解
庸者的救赎2015-11-20 02:30:23 AFN那个使用的时候不需要弱引用的,因为从你的封装方式来看,那个block并不会被当前视图控制器持有,而是被manager持有了,所以不需要__wea ...
- iOS无处不在详解iOS集成第三方登录(SSO授权登录无需密码)
链接地址:http://www.it165.net/pro/html/201408/18884.html 1.前言 不多说,第三登录无处不在!必备技能,今天以新浪微博为例. 这是上次写的iOS第三方社 ...
- iOS中 蓝牙2.0详解/ios蓝牙设备详解 韩俊强的博客
每日更新关注:http://weibo.com/hanjunqiang 新浪微博 整体布局如下: 程序结构如右图: 每日更新关注:http://weibo.com/hanjunqiang ...
随机推荐
- Learning Context Graph for Person Search
Learning Context Graph for Person Search 2019-06-24 09:14:03 Paper:http://openaccess.thecvf.com/cont ...
- 【mybatis源码学习】ResultMap查询结果映射
一.ResultMap包含的元素 constructor - 用于在实例化类时,注入结果到构造方法中 idArg - ID 参数:标记出作为 ID 的结果可以帮助提高整体性能 arg - 将被注入到构 ...
- GIS地理处理脚本案例教程——批量栅格分割-批量栅格裁剪-批量栅格掩膜-深度学习样本批量提取
GIS地理处理脚本案例教程--批量栅格分割-批量栅格裁剪-批量栅格掩膜-深度学习样本批量提取 商务合作,科技咨询,版权转让:向日葵,135-4855_4328,xiexiaokui#qq.com 关键 ...
- DispatcherServlet 被加载顺序
javax.servlet.Servlet#init 0 -> javax.servlet.GenericServlet#init(javax.servlet.ServletConfig) 1 ...
- SEO前端篇(一)页面布局
由于工作需要,在此对seo的一些相关知识做一个总结. 首先要了解必要的基础知识,什么是SEO以及搜索引擎的工作原理.这样才能继续下面的话题. 一.SEO定义 SEO全称:Search English ...
- Hashmap(类似字典的东西)
注意: 键值是唯一的,1个键对应一个值 常用api 打印处字典直接println方法 判断是否存在key值 containsKey() 例子: 基础操作 https://ke.qq.com/w ...
- EasyDSS高性能RTMP、HLS(m3u8)、HTTP-FLV、RTSP流媒体服务器同步输出http-flv协议流是怎么实现的?
http-flv是什么 http-flv是以http为传输协议,flv媒体格式为内容的方式实时下载flv音视频帧.为什么选择flv格式而非mp4?原因是mp4必须要有moov box或者moof bo ...
- NODE_ENV=production 环境变量设置
"start": "NODE_ENV=production node ./bin/www"
- Deploy != Release(第一部分):部署与发布的区别,以及为什么这很重要
原文地址:http://ju.outofmemory.cn/entry/351873 翻译自: Deploy != Release (Part 1): The difference between d ...
- 使用Nginx+WordPress搭建个人网站
背景 很多研究技术的朋友喜欢写博客.如果希望搭建一个完全属于自己的网站,也并不困难.这里简要分享一下我搭建这个博客网站的经验. 关键步骤 购买服务器.域名.DNS云解析服务 网站备案(可选) 安装Ng ...