iOS UIKit:viewController之Present (3)
弹出和转换view controller技术是一种快速且简单的方式将新view content展示在屏幕中。目前有两种方式弹出新的view controller:Present方式和segues方式。本文介绍Present方式,下文介绍segues方式。
存在两种方式将新view controller显示在屏幕中:Container 方式和present(弹出)方式。Present方式是由UIViewController类自身支持的,所以任何view controller对象都可用。弹出view controller的过程就是创建原始view controller和新view controller之间的关系,其中将原始view controller称作presenting view controller(主动),而将新的view controller称作presented view controller(被动)。
1.样式
1.1 Presentation样式
view controller的Presentation(弹出)样式控制着view controller出现在屏幕的行为,UIKit定义了很多种弹出样式,用户也可以自定义。若需改变默认的弹出样式,可修改新view controller (UIViewController对象)的modalPresentationStyle属性即可。
1) Full-Screen样式
Full screen样式会覆盖整个屏幕,从而阻止与底层视图内容进行交互。其中在屏幕处于水平环境下,只有一种Full screen样式会覆盖整个屏幕,其它样式只是覆盖了底层view controller的部分内容,而没有覆盖的部分则处于模糊状态。
如图 31所示,是在horizontally regular环境下使用UIModalPresentationFullScreen,UIModalPresentationPageSheet,和UIModalPresentationFormSheet样式的例子。其中左上绿色的view controller弹出了右上蓝色的view controller,然后有最底下三种样式的不同效果。

图 31 The full screen presentation styles
注意:
当使用了UIModalPresentationFullScreen样式弹出一个view cotroller时,UIKit正常会移除底层的view cotroller,当然可以将其改为UIModalPresentationOverFullScreen样式,从而阻止移除操作。当新弹出的view controller有透明的部分,就应该使用UIModalPresentationOverFullScreen样式。
2) Popover样式
以Popover样式弹出view controller是使用了UIModalPresentationPopover属性值。在horizontally regular环境下,popover view只是覆盖了部分屏幕,如图 32所示。在horizontally compact环境下,popover默认采用UIModalPresentationOverFullScreen样式。若在弹出view controller之后,触摸popover view以外部分,则被弹出view controller将自动消失。

图 32 The popover presentation style
因为在horizontally compact环境下,popover样式是覆盖的整个屏幕,所以需要修改代码来适配。
3) Current Context样式
使用UIModalPresentationCurrentContext样式可以覆盖指定的view controller。当使用这种样式,那么需要设置覆盖 view controller的definesPresentationContext属性为YES。如图 33所示,只覆盖split view controller的其中一个child view cotroller。

图 33 The current context presentation style
当转换到horizontally compact环境时,那么current context样式将采用UIModalPresentationFullScreen样式。
4) Custom样式
当使用自定义的样式,可以设置view controller为UIModalPresentationCustom 样式。当创建自定义样式时,将调用UIPresentationController子类,并且使用某种动画方式展示自定义view到屏幕中,及设置新弹出的view controller的尺寸和位置。
1.2 Transition样式
Transition样式决定了新view controller的动画方式,可以使用内置的标准Transition样式来设置新view controller的modalTransitionStyle属性。当弹出view controller时,UIKit会创建一个相应动画。如图 34所示,是使用了UIModalTransitionStyleCoverVertical属性值来弹出view controller例子,B view controller从底部向上升起。

图 34 A transition animation for a view controller
1.3 Present与Show
UIViewController类提供两种方式来显示view controller:
- showViewController:sender和showDetailViewController:sender方法:这两个方法让新的view controller来决定怎样最好的处理present操作;
- presentViewController:animated:completion方法:该方法总是以模态的形式弹出view controller。
2 Present
2.1 Show
使用showViewController和showDetailViewController方法可以将view controller展示在屏幕中,其操作步骤非常简单:
a) 创建一个被展示的view controller,并对其进行初始化;
b) 设置这个被展示view controller的modalPresentationStyle属性;
c) 置这个被展示view controller的modalTransitionStyleproperty属性;
d) 调用当前view controller的showViewController或showDetailViewController方法。
如下所示:
1 - (IBAction)segueDown:(id)sender {
2 firstViewController *firstVC = [self.storyboard instantiateViewControllerWithIdentifier:@"firstViewController"];
3 firstVC.modalPresentationStyle = UIModalPresentationFullScreen;
4 firstVC.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
5
6 [self showViewController:firstVC sender:sender];
7 }
当采用Present模型弹出一个view controller时,应该告诉UIKit想弹出的view controller是什么,并且还需告诉以什么动画样式展示。其使用过程为:
a) 创建一个被展示的view controller,并对其进行初始化;
b) 设置这个被展示view controller的modalPresentationStyle属性;
c) 置这个被展示view controller的modalTransitionStyleproperty属性;
d) 调用当前view controller的presentViewController:animated:completion:方法。
如下所示:
1 - (IBAction)PresentDown:(id)sender {
2 firstViewController *firstVC = [self.storyboard instantiateViewControllerWithIdentifier:@"firstViewController"];
3 firstVC.modalPresentationStyle = UIModalPresentationFullScreen;
4 firstVC.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
5
6 [self presentViewController:firstVC animated:YES completion:nil];
7 }
2.3 Present(Popover)
若是以Popover样式弹出view controller,则需要进行一些配置,因为默认会被配置为UIModalPresentationOverFullScreen样式。其中配置过程为:
a) 创建一个被展示的view controller,并对其进行初始化。
b) 设置这个新view controller的modalPresentationStyle属性为UIModalPresentationPopover值。
c) 获取新view controller的popoverPresentationController属性,即UIPopoverPresentationController对象。
d) 设置UIPopoverPresentationController对象中的preferredContentSize属性值,即为弹出view cotroller窗口的显示大小。 设置弹出的锚点,其中有两种方式:
若是bar button item,则设置UIPopoverPresentationController对象的barButtonItem属性;
若是普通视图,则设置UIPopoverPresentationController对象的sourceView和sourceRect属性。
e) 设置UIPopoverPresentationController对象的delegate,并且该delegate需要实现两个方法:adaptivePresentationStyleForPresentationController和popoverPresentationControllerShouldDismissPopover。
f) 调用当前view controller的presentViewController:animated:completion:方法。
如下是两种不同锚点的设置方法:
1) 导航工具栏
若是在工具栏按钮中弹出Popover,则设置UIPopoverPresentationController对象的barButtonItem属性为相应工具栏项,该项只是显示箭头显示的起始位置。
1 - (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller traitCollection:(UITraitCollection *)traitCollection //实现delegate的两个方法
2 {
3 return UIModalPresentationNone;
4 }
5 - (BOOL)popoverPresentationControllerShouldDismissPopover:(UIPopoverPresentationController *)popoverPresentationController
6 {
7 return YES;
8 }
9 - (IBAction)PopoverDown:(id)sender //该方法为按钮的响应方法。
10 {
11 firstViewController *firstVC = [self.storyboard instantiateViewControllerWithIdentifier:@"firstViewController"];
12 firstVC.modalPresentationStyle = UIModalPresentationPopover;
13 firstVC.preferredContentSize = CGSizeMake(100, 150); //设置弹出窗口的长宽值,可选
14
15 UIPopoverPresentationController *popvc = [firstVC popoverPresentationController];
16 popvc.barButtonItem = _navigation.rightBarButtonItem;
17 popvc.permittedArrowDirections = UIPopoverArrowDirectionUp; //设置弹出窗口的箭头方向,可选
18 popvc.delegate = self; //必须设置委托对象
19
20 [self presentViewController:firstVC animated:YES completion:nil];
21 }

图 35 导航工具栏Popover弹出效果图
2) 按钮
也可将锚点设置在按钮中,其它操作方法相同,也需要实现delegate,如下的delegate与上述一样,如下所示。
1 - (IBAction)PopoverDown:(id)sender //该方法为按钮的响应方法。
2 {
3 firstViewController *firstVC = [self.storyboard instantiateViewControllerWithIdentifier:@"firstViewController"];
4 firstVC.modalPresentationStyle = UIModalPresentationPopover;
5 firstVC.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
6 // firstVC.preferredContentSize = CGSizeMake(100, 150);
7
8 UIPopoverPresentationController *popvc = [firstVC popoverPresentationController];
9 popvc.permittedArrowDirections = UIPopoverArrowDirectionDown; //与上述的箭头方向不同
10 popvc.sourceView = _popoverButton;
11 popvc.sourceRect = _popoverButton.bounds;
12 popvc.delegate = self;
13
14 [self presentViewController:firstVC animated:YES completion:nil];
15 }

图 36 按钮位置的Popover的显示效果图
3 Dismiss
可以清除一个已经被弹出的view controller,只需调用原来view controller中的dismissViewControllerAnimated:completion方法。当然也可以调用被弹出view controller的dismissViewControllerAnimated:completion方法,一样可以返回到弹出view controller的状态。
如下是在被弹出view controller的按钮响应方法中:
- (IBAction)dismissDown:(id)sender
{
[self dismissViewControllerAnimated:YES completion:nil];
}
4 Storyboard间
在同一个Storyboard中,可以使用segues来完成不同view controller的弹出操作;但在不同的Storyboard之间是无法使用segues完成弹出操作的。但以Programing方式,是可以实现不同Storyboard间的view controller的弹出操作。
如下所示:
1 UIStoryboard* sb = [UIStoryboard storyboardWithName:@"SecondStoryboard" bundle:nil];
2 MyViewController* myVC = [sb instantiateViewControllerWithIdentifier:@"MyViewController"];
3 [self presentViewController:myVC animated:YES completion:nil]; // Display the view controller
iOS UIKit:viewController之Present (3)的更多相关文章
- iOS UIKit:viewController之动画(5)
当弹出一个view controller时,UIKit提供了一些标准转换动画,并且也支持用户自定义的动画效果. 1 UIView动画 UIView是自带动画实现功能,其中有两种方式实现: ...
- iOS UIKit:viewController之层次结构(1)
ViewController是iOS应用程序中重要的部分,是应用程序数据和视图之间的重要桥梁.且应用程序至少有一个view controller.每个view controller对象都负责和管理一个 ...
- iOS UIKit:viewController之定义(2)
@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...
- iOS UIKit:viewController之Segues (4)
@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...
- iOS UIKit:Navigation Controllers
navigation controller是一种层次结构的container view controller,即其通过一个view controllers栈来管理内部的content view con ...
- iOS UIKit:view
@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css); @import url(/ ...
- iOS UIKit Dynamics入门 UIKit动力学,实现重力、连接、碰撞、悬挂等动画效果
本文为转载文章 版权归原文所有 什么是UIKit动力学(UIKit Dynamics) 其实就是UIKit的一套动画和交互体系.我们现在进行UI动画基本都是使用CoreAnimation或者UIVie ...
- [Android开发学iOS系列] ViewController
iOS ViewController 写UIKit的代码, ViewController是离不开的. 本文试图讲讲它的基本知识, 不是很深入且有点杂乱, 供初级选手和跨技术栈同学参考. What is ...
- iOS UIKit:TableView之编辑模式(3)
一般table view有编辑模式和正常模式,当table view进入编辑模式时,会在row的左边显示编辑和重排控件,如图 42所示的编辑模式时的控件布局:左边的editing control有表 ...
随机推荐
- MVC简单分页(未实现无刷新分页)
分页Html辅助方法 using System.Text; using System.Web: using System.Web.Mvc; namespace System.Web.Mvc { pub ...
- bzoj 1414: [ZJOI2009]对称的正方形 manacher算法+單調隊列
1414: [ZJOI2009]对称的正方形 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 331 Solved: 149[Submit][Stat ...
- Immutable Object模式
多线程共享变量的情况下,为了保证数据一致性,往往需要对这些变量的访问进行加锁.而锁本身又会带来一些问题和开销.Immutable Object模式使得我们可以在不使用锁的情况下,既保证共享变量访问的线 ...
- Unity与IOS交互
Unity IOS交互 @By 广州小龙 QQ群:63438968 环境:Mac os 10.9.2 Unity 4.2.1f4 Xcode 5.0.2 Unity IOS的交互我写过一个教程 ...
- java线程:Atomic(原子的)
一.何谓Atomic? Atomic一词跟原子有点关系,后者曾被人认为是最小物质的单位.计算机中的Atomic是指不能分割成若干部分的意思.如果一段代码被认为是Atomic,则表示这段代码在执行过程中 ...
- 深入浅出Node.js (附录C) - Node编码规范
C.1 根源 C.2 编码规范 C.2.1 空格与格式 C.2.2 命名规范 C.2.3 比较操作 C.2.4 字面量 C.2.5 作用域 C.2.6 数组与对象 C.2.7 异步 C.2.8 类与模 ...
- Solr -- Solr Facet 2
solr将以导航为目的的查询结果称为facet. 它并不会修改查询结果信息, 只是在查询结果上根据分类添加了count信息, 然后用户根据count信息做进一步的查询, 比如淘宝的查询列表中, 上面会 ...
- HDOJ/HDU 2555 人人都能参加第30届校田径运动会了(判断加排序~)
Problem Description 杭州师范大学第29届田径运动会圆满的闭幕了,本届运动会是我校规模最大,参赛人数最多的一次运动会.在两天半时间里,由学生.教工组成的61支代表队共2664名运动员 ...
- 如何理解这段代码:void (*signal (int sinno,void(*func)(int)))(int)
void (*signal (int sinno,void(*func)(int)))(int) 先来看void(*func)(int) 这里的意思是声明一个函数指针func,它的参数类型为int ...
- jQuery 遍历
.add() add() 方法将元素添加到匹配元素的集合中 .add(selector)//字符串值,表示查找供添加到匹配元素集合的元素的选择器表达式. .add(elements)//添加到匹配元素 ...