子视图控制器旋转

子视图控制器旋转

本文介绍了未调用子视图控制器旋转方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将子视图控制器添加到父视图控制器,并让父视图控制器通知子视图控制器旋转事件。但是,旋转消息未转发到子视图控制器(这是默认行为)。为什么没有发生这种默认行为?

I am attempting to add a child view controller to a parent view controller, and have the parent view controller inform the child view controller of rotation events. However, the rotation messages are not being forwarded to the child view controller (which is the default behavior). Why is this default behavior not occurring?

环境:iOS 7,XCode 5,OSX 10.9

Environment: iOS 7, XCode 5, OSX 10.9

我正在按照Apple文档中的说明实现自定义容器视图控制器:。我试图建立一个转发轮换事件的简单父子关系。总层次结构完全如图14-1重新绘制的文档中所示:

I am implementing a custom container view controller following instructions in the Apple Document: "Creating Custom Container View Controllers". I am attempting to establish a simple parent child relationship that forwards rotation events. The total hierarchy is exactly as pictured in the documentation figure 14-1 redrawn here:

ParentViewController --------> RootView
        |                       /   \
        |                      /     \
        ˅                     /       \
ChildViewController ---> ChildView     \
                                        \
                                    OverLayView

我正在使用代码完成此操作来自于parentViewController中的清单4-1的文档(unityViewController = childViewController,unityView = childView):

I am accomplishing this using the code from the document Listing 4-1 in the parentViewController (unityViewController = childViewController, unityView = childView):

// In the ParentViewController
// Called by the Application Delegate in application:didFinishLaunchingWithOptions:
- (void)addUnityViewController:(UIViewController *)unityViewController withUnityView:(UIView *)unityView
{
    [self addChildViewController:unityViewController]; // 1. Establish Child parent relationship

    unityView.frame = self.view.frame;                 // 2. Set the frame (explicitly or with constraints)
    [self.view addSubview:unityView];                  // 2.1 Add the subview AFTER you set the frame
    ... // add some view constraints here

    [self.view sendSubviewToBack:unityView];           // In the back, but not completely obstructed by any other views
    [unityViewController didMoveToParentViewController:self];// 3. Tell the child what happened
}

此代码成功地将子视图显示为RootView的子视图,其中OverlayView添加了一些功能按钮。但是,当设备旋转时,父视图控制器会成功旋转其视图,但不会将旋转消息转发到子视图控制器(unityViewController),从而导致rootView中显示不正确的childView(unityView)。根据,这应该会自动发生:

This code successfully displays the child view as a subview of the RootView with the OverlayView adding some functional buttons. However, when the device rotates, the parent view controller rotates its views successfully, but does not forward the rotation messages to the child view controller (unityViewController) resulting in an improperly displayed childView (unityView) in the rootView. According to "Creating Custom Container View Controllers" this should happen automatically:

为了确保这一点发生,我改写了以下方法:

To ensure that this should be happening, I overrode the following methods:

// In the parent ViewController:
- (BOOL)shouldAutorotate
{
    return YES;
}

- (BOOL)shouldAutomaticallyForwardAppearanceMethods
{
    return YES;
}

- (BOOL)shouldAutomaticallyForwardRotationMethods
{
    return YES;
}

然而, willRotateToInterfaceOrientation:duration:和子 viewController(unityViewController)中的strong> didRotateFromInterfaceOrientation:方法。我已经确认确实正在调用方法,在那些我调用 super 的方法中,但是从不调用子方法。

However, the willRotateToInterfaceOrientation:duration: and didRotateFromInterfaceOrientation: methods in the child viewController (unityViewController) are never being called. I have been able to confirm that the parent methods are indeed being called, and in those I make the call to super, but the child methods are never called.

这种默认行为没有发生的原因是什么?我需要父和子都接收旋转消息以便正确显示。

What is the reason that this default behavior is not occurring? I need both the parent and the child to receive the rotation messages for proper display.

注意:我知道我可以在父级中手动调用这些方法,但我不想手动制作这些调用并添加应该是默认行为的额外代码。

Note: I am aware that I can manually make the calls to these methods in the parent, but I do not want to manually make these calls and add extra code that should be default behavior.

非常感谢您的帮助!

推荐答案

为了让您的子视图控制器接收转发的方向回调,它必须是可见的。您的子视图控制器可能不可见,因为它的视图尚未添加到视图层次结构中。

In order for your child view controller to receive forwarded orientation callbacks, it must be visible. It's likely that your child view controller isn't visible because its view hasn't been added into the view hierachy.

如果您将方法更改为此,则应该启动看到你的旋转消息被调用:

If you change your method to this, you should start seeing your rotation messages being called:

- (void)addUnityViewController:(UIViewController *)unityViewController
{
    [self addChildViewController:unityViewController];

    unityViewController.view.frame = self.view.bounds;
    [self.view addSubview:unityViewController.view];
    ... // add some view constraints here

    [self.view sendSubviewToBack:unityViewController.view];
    [unityViewController didMoveToParentViewController:self];
}

更多信息: 。

这篇关于未调用子视图控制器旋转方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 00:53