模态

2020-01-17  本文已影响0人  wpf_register

原文链接

iOS中显示ViewController的方式有两种push和modal,modal也叫模态,其主要用于有以下场景:

  • 收集用户输入信息
  • 临时呈现一些内容
  • 临时改变工作模式
  • 显示一个新的view层级

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包括 UINavigation Controller, UISplitView Controller, 以及 UIPageViewController.

ModalPresentationStyle

UIModalTransitionStyle

presentation context

presentation context是指为本次present提供上下文环境的类,
需要指出的是,presenting VC通常并不是presentation context,
当我们需要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的方式如下:
一个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

当然,还有另外一种特殊情况,当我们在一个presented VC上再present一个VC时,UIKit会直接将这个presented VC做为presentation context。

上一篇 下一篇

猜你喜欢

热点阅读