我有一个 UIViewController,我想将它作为一个独立层添加到整个应用程序之上.所以我尝试在应用程序委托中使用 [self.window addSubview:viewController.view] 将它添加到 UIWindow.
I have a UIViewController that I want to add on top of the entire app as an independent layer. So I tried adding it to the UIWindow using [self.window addSubview:viewController.view] in the app delegate.
但是,当用户旋转设备时,我的视图控制器中没有得到 willRotateToInterfaceOrientation 事件.所以我尝试按照 here 的建议进行操作:我添加了我的视图控制器作为应用程序委托中窗口的根视图控制器的子视图,但我的 willRotateToInterfaceOrientation 仍然没有被调用.
However, when the user rotates the device, I do not get the willRotateToInterfaceOrientation event in my view controller. So I tried doing what was suggested here: I added my view controller as a subview of the root view controller of the window in the app delegate, but my willRotateToInterfaceOrientation is still not being called.
我该怎么办?
您的 rootViewController 正在充当容器视图控制器.
Your rootViewController is acting as a container view controller.
在 iOS 5.0 及更高版本中,自定义 UIViewController 子类现在可以充当容器视图控制器.UINavigationController 和 UITabBarController 类是 UIKit 提供的容器视图控制器的示例.容器视图控制器背后的想法是它管理来自其包含的视图控制器的内容的呈现,也称为其子视图控制器.子内容可以按原样呈现,也可以与容器视图控制器管理的其他自定义视图一起呈现.
In iOS 5.0 and later, custom UIViewController subclasses can now act as container view controllers. The UINavigationController and UITabBarController classes are examples of container view controllers provided by UIKit. The idea behind a container view controller is that it manages the presentation of the content from its contained view controllers, also known as its child view controllers. The child content can be presented as-is or in conjunction with other other custom views managed by the container view controller.
您正在做的是实现自己的视图层次结构,因此您负责沿着该层次结构向下传递 UILifecycle-Events.iOS >= 5.0 支持你创建这样的容器视图控制器,提供了
What you are doing is implementing an own view hierarchy, so you are responsible for passing the UILifecycle-Events down along that hierarchy. iOS >= 5.0 supports you creating such container viewControllers, offering the
- (void)addChildViewController:(UIViewController *)childController
- (BOOL)automaticallyForwardAppearanceAndRotationMethodsToChildViewControllers
方法(以及更多).对于 iOS <5.0 很遗憾,您将不得不手动执行此操作.
methods (and more). For iOS < 5.0 you will unfortunately have to do this manually.
这篇关于根 ViewController 内的 UIViewController 不旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!