Present ViewController Modally

一、主要用途

  弹出模态ViewController是IOS变成中很有用的一个技术,UIKit提供的一些专门用于模态显示的ViewController,如UIImagePickerController等。弹出模态ViewController主要使用于一下这几种情形:

  1、收集用户输入信息

  2、临时呈现一些内容

  3、临时改变工作模式

  4、相应设备方向变化(用于针对不同方向分别是想两个ViewController的情况)

  5、显示一个新的view层级

  这几种情形都会暂时中断程序正常的执行流程,主要作用是收集或者显示一些信息。

二、几个概念和常用设置

1、presenting view controller Vs presented view controller

  当我们在view controller A中模态显示view controller B的时候,A就充当presenting view controller(弹出VC),而B就是presented view controller(被弹出VC)。官方文档建议这两者之间通过delegate实现交互,如果使用过UIImagePickerController从系统相册选取照片或者拍照,我们可以发现imagePickerController和弹出它的VC之间就是通过UIImagePickerControllerDelegate实现交互的。因此我们在实际应用用,最好也遵守这个原则,在被弹出的VC中定义delegate,然后在弹出VC中实现该代理,这样就可以比较方便的实现两者之间的交互。

2、Modal Presentation Styles(弹出风格)

  通过设置presented VC的modalPresentationStyle属性,我们可以设置弹出View Controller时的风格,有以下四种风格,其定义如下:

typedef enum {
UIModalPresentationFullScreen = 0,
UIModalPresentationPageSheet,
UIModalPresentationFormSheet,
UIModalPresentationCurrentContext,
} UIModalPresentationStyle;

  UIModalPresentationFullScreen代表弹出VC时,presented VC充满全屏,如果弹出VC的wantsFullScreenLayout设置为YES的,则会填充到状态栏下边,否则不会填充到状态栏之下。

  UIModalPresentationPageSheet代表弹出是弹出VC时,presented VC的高度和当前屏幕高度相同,宽度和竖屏模式下屏幕宽度相同,剩余未覆盖区域将会变暗并阻止用户点击,这种弹出模式下,竖屏时跟UIModalPresentationFullScreen的效果一样,横屏时候两边则会留下变暗的区域。

  UIModalPresentationFormSheet这种模式下,presented VC的高度和宽度均会小于屏幕尺寸,presented VC居中显示,四周留下变暗区域。

  UIModalPresentationCurrentContext这种模式下,presented VC的弹出方式和presenting VC的父VC的方式相同。

  这四种方式在iPad上面统统有效,但在iPhone和iPod touch上面系统始终已UIModalPresentationFullScreen模式显示presented VC。

3、Modal Transition Style(弹出时的动画风格)

  通过设置设置presented VC的modalTransitionStyle属性,我们可以设置弹出presented VC时场景切换动画的风格,其定义如下:

typedef enum {
UIModalTransitionStyleCoverVertical = 0,
UIModalTransitionStyleFlipHorizontal,
UIModalTransitionStyleCrossDissolve,
UIModalTransitionStylePartialCurl,
} UIModalTransitionStyle;

  我们可以看到有从底部滑入,水平翻转进入,交叉溶解以及翻页这四种风格可选。这四种风格在不受设备的限制,即不管是iPhone还是iPad都会根据我们指定的风格显示转场效果。

4、Dismiss Modal ViewController(消失弹出的VC)

  消失presented VC,我们可以通过调用以下两个函数中的任何一个来完成

dismissModalViewControllerAnimated:                 // 将要废弃,不赞成继续使用
dismissViewControllerAnimated:completion:

  谁来调用这消失presented VC的这个方法:正确的做法是“谁污染谁治理”,即presenting VC调用上面的方法来取消presented VC的显示。这样做有一个好处,如果一个VC真不用户做的不同选择可能弹出不同的view controller,当不再需要显示被弹出的view controller的时候,直接调用[self dismissModalViewControllerAnimated]即可使之消失,而不用去关心其具体显示的哪一类view controller。当然系统在这里做了优化,当我们在presented VC里面调用上面的方法的时候,系统会自动的将这个消息传递到相应的presenting VC中,这样就可以实现不管谁弹出了自己,当不再需要的时候直接将自己消失掉的功能。在应用中具体要采用那种要看具体情况,如果presented VC需要和presenting VC有数据传递的话,建议在presenting VC实现的代理函数中dismiss弹出的view controller

presenting view controller的更多相关文章

  1. Model View Controller

    On the iPhone or iPod touch, a modal view controller takes over the entire screen. This is the defau ...

  2. 【IOS笔记】View Controller Basics

    View Controller Basics   视图控制器基础 Apps running on iOS–based devices have a limited amount of screen s ...

  3. View Controller Transition:京东加购物车效果

    冬天已经过去了,阳光越来越暖洋洋的了.还记得上学的时候,老师总说"春天是播种的季节",而我还没在朋友圈许下什么愿望.一年了,不敢想象回首还能看到点什么,所以勇往直前.当被俗世所扰, ...

  4. View Controller Programming Guide for iOS---(四)---Creating Custom Content View Controllers

    Creating Custom Content View Controllers 创建自定义内容视图控制器 Custom content view controllers are the heart ...

  5. View Controller Programming Guide for iOS---(二)---View Controller Basics

    View Controller Basics Apps running on iOS–based devices have a limited amount of screen space for d ...

  6. View Controller Programming Guide for iOS---(一)---About View Controllers

    About View Controllers View controllers are a vital link between an app’s data and its visual appear ...

  7. View Controller Relationships

    Parent-child relationshipsParent-child relationships are formed when using view controller container ...

  8. iOS7之定制View Controller切换效果

    在iOS5和iOS6前,View Controller的切换主要有4种: 1. Push/Pop,NavigationViewController常干的事儿 2. Tab,TabViewControl ...

  9. 自己定义View Controller转换动画

    原文链接 : Introduction to Custom View Controller Transitions and Animations 原文作者 : joyce echessa 译文出自 : ...

随机推荐

  1. 洛谷P3704 [SDOI2017]数字表格(莫比乌斯反演)

    传送门 yyb大佬太强啦…… 感觉还是有一点地方没有搞懂orz //minamoto #include<cstdio> #include<iostream> #include& ...

  2. 在VMware上克隆Linux虚拟机及其网卡配置方法

    最近在搭建Hadoop集群,1个Master,3个Workers.使用VMware workstations创建Linux虚拟机,版本是CentOS7.安装完成并做了相应的网络配置后,使用VMware ...

  3. Spring IOC 的源码分析

    刚学习Spring的时候,印象最深的就是 DispatcherServlet,所谓的中央调度器,我也尝试从这个万能胶这里找到入口 configureAndRefreshWebApplicationCo ...

  4. excel 公式2列合并

    =A2&"="&C2 ="UPDATE comm_department SET parent_id='"&D2&"' ...

  5. C# 数组之ArrayList

    一.引言 ArrayList类相当于一种高级的动态数组,是Array类的升级版本. 一般的Array,底层是数组实现的,对于数据的查找和修改十分高效.但是有2个大的缺点,其一为增删低效,其二为数组长度 ...

  6. ES6新特性使用小结(一)

    一.let const 命令 'use strict'; /*function test(){ //let a = 1; for(let i=1;i<3;i++){ console.log(i) ...

  7. Codeforces Round #564 (Div. 2) C. Nauuo and Cards

    链接:https://codeforces.com/contest/1173/problem/C 题意: Nauuo is a girl who loves playing cards. One da ...

  8. 如何使用在Windows 下AspNetCore Api 和 consul

    在Windows 下如何使用 AspNetCore Api 和 consul https://blog.csdn.net/sD7O95O/article/details/80750803 一.概念:什 ...

  9. 牛客网Java刷题知识点之抽象类与接口

    不多说,直接上干货! 接口和内部类为我们提供了一种将接口与实现分离的更加结构化的方法. 抽象类与接口是Java语言中对抽象概念进行定义的两种机制,正是由于它们的存在才赋予java强大的面向对象的能力. ...

  10. Nginx中的Rewrite的重定向配置与实践

    阅读目录 一:理解地址重写 与 地址转发的含义. 二:理解 Rewrite指令 使用 三:理解if指令 四:理解防盗链及nginx配置   简介:Rewrite是Nginx服务器提供的一个重要的功能, ...