UIViewController inside a root ViewController not

2019-02-18 03:54发布

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.

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.

What should I do?

2条回答
地球回转人心会变
2楼-- · 2019-02-18 04:31

Your rootViewController is acting as a container view controller.

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.

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

methods (and more). For iOS < 5.0 you will unfortunately have to do this manually.

查看更多
Juvenile、少年°
3楼-- · 2019-02-18 04:32

In your rootViewController you should hold a reference to your independentLayerController and forward the willRotateToInterfaceOrientation.

Your rootViewController will receive the messages and you can call willRotateToInterfaceOrientation on the independentLayerController.

查看更多
登录 后发表回答