14

Given the below code

self.view.backgroundColor = [UIColor yellowColor];
MyViewController *myVC = [[MyViewController alloc] initWithNibName:@"MyView" bundle:nil]
myVC.view.backgroundColor = [UIColor clearColor];
myVC.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentViewController:myVC animated:NO completion:nil];

What happens under the hood when we call presentViewController ? When myVC is visible I cannot see yellow color, then I checked myVC.view.superView in it's viewDidAppear method and it is UIWindow.

Q1. Is that mean until the modal window is up presentingViewController.view (self.view in above case) is removed from the View hierarchy and presentedViewController.view (myVC.view in above case) is added over UIWindow ?

Q2. What will be the case if myVC.modalPresentationStyle != UIModalPresentationFullScreen ?

Q3. Does iOS also remove all the views from UIWindow except presentedViewController.view until the full screen modal dialog is up for optimization ? If NO why not ?

4

1 回答 1

18

首先,让我们讨论没有动画的情况。

打电话之前present

  1. 你的窗口有一个视图层次结构,从它的rootViewController视图开始。

打电话后present:

  1. 视图层次结构仍然存在而没有更改。
  2. 一个特殊的全屏视图称为“调光视图”被添加到窗口中(也就是说,不是在rootViewController的视图内,而是在窗口内(窗口也是一个UIView)。这个视图是透明的,使呈现控制器变暗并阻止用户相互作用。
  3. 然后将呈现的(模态)控制器的视图也添加到窗口中。

在窗口和呈现的控制器窗口之间添加了一些其他视图。如果您记录视图层次结构,您将看到已命名的类_ControllerWrapperView或类似名称。但是,这在 iOS 版本之间发生了变化,您不应该依赖视图结构。请注意,模态控制器永远不可能是透明的,因为它不是窗口的直接子视图,并且控制器和窗口之间的包装器是不透明的。

动画案例几乎相同。只有步骤之间有一些花哨的动画。

编辑2: 答案真的有点不正确。iPhone 和 iPad 提供的控制器之间存在很大差异。

在 iPhone 上,呈现的控制器始终全屏显示,而呈现的控制器实际上已从窗口中移除。

在 iPad 上,如果呈现的控制器不是全屏的(请参阅 参考资料UIModalPresentationStyle),呈现的控制器会停留在窗口中。

你的问题:

这是否意味着在模态窗口启动之前,从 View 层次结构中删除 presentingViewController.view (在上述情况下为 self.view )并在 UIWindow 上添加presentingViewController.view (在上述情况下为myVC.view )?

如果控制器是全屏的,那么这个说法是正确的。否则,呈现视图控制器会保留在那里,但整个内容会被其他视图重叠(即使它们是半透明的)。此外,呈现的和呈现的控制器视图之间总是存在一些视图。

如果 myVC.modalPresentationStyle != UIModalPresentationFullScreen 会怎样?

请参阅上一个问题的答案 - 在 iPhone 上,没有区别。

iOS 是否还会从 UIWindow 中删除除presentedViewController.view 之外的所有视图,直到全屏模式对话框进行优化?如果没有,为什么不呢?

从我的测试中,只有呈现控制器从窗口层次结构中删除。这可能是为了优化绘图性能。这是系统可以安全移除的唯一控制器。删除任何其他视图可能会导致问题(例如,应该始终可见的视图)。

编辑: 如果你想制作一个透明的控制器,你可以:

  1. +[UIView transition...]使用过渡动画 ( )将视图直接添加到您的视图层次结构(控制器的视图或窗口)
  2. 相同,但也将子控制器添加到您的控制器。
于 2013-04-28T18:39:56.017 回答