自定义transitionFromViewController方法抛出错误

4
我需要在我的导航控制器中创建一个类似于pushViewController:viewController animated:YES的方法,但我需要在视图控制器的框架顶部添加80像素,同时进行从左到右的过渡动画。然而,下面的方法会抛出以下错误:
“Parent view controller is using legacy containment in call to -[UIViewController transitionFromViewController:toViewController:duration:options:animations:completion:]”
- (void)transitionFrom:(UIViewController *)oldController To:(UIViewController *)newController
{
    [self addChildViewController:newController];
    //[self configureChild:newController];
    [newController.view setFrame:CGRectMake(320, 80, oldController.view.frame.size.width, oldController.view.frame.size.height)];
    [self transitionFromViewController:oldController
                      toViewController:newController
                              duration:0.5
                               options:UIViewAnimationOptionTransitionFlipFromLeft
                            animations:^{
                                [oldController.view setFrame:CGRectMake(-320, 80, oldController.view.frame.size.width, oldController.view.frame.size.height)];
                                [newController.view setFrame:CGRectMake(0, 80, newController.view.frame.size.width, newController.view.frame.size.height)];
                            }
                            completion:^(BOOL finished){
                                //[oldController willMoveToParentViewController:nil];
                                [oldController removeFromParentViewController];
                                [newController didMoveToParentViewController:self];
                            }];
}
3个回答

4

哇,我也花了几个小时来解决这个问题。

这是我发现的,希望能帮助其他人。

我没有从UINavigationController中调用transitionFromViewController,但仍然收到了相同的奇怪消息。

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', 
reason: 'Parent view controller is using legacy containment in call to 
 -[UIViewController transitionFromViewController:toViewController:duration:options:animations:completion:]'

我终于发现我的问题是在我的容器UIViewController子类中覆盖了这个方法:

-(BOOL)shouldAutomaticallyForwardAppearanceMethods
{
    return NO;
}

这个通常默认为YES,除非你想自己处理它(我受到了苹果WWDC2012 MediaNote示例代码的启发...)。

如果你想要transitionFromViewController正常工作,那么shouldAutomaticallyForwardAppearanceMethods应该返回YES。


出现了相同的错误。我正在从UINavigationController调用transitionFromViewController,我已经重写了shouldAutomaticallyForwardAppearanceMethods并返回了YES。仍然是相同的错误。 :( - JiteshW

4
你是否在UINavigationController上调用了transitionFromViewController方法?(self是UINavigationController的实例吗?)因为如果你在UINavigationController上调用transitionFromViewController方法,就会出现这个错误。苹果文档指出,该方法只适用于自定义容器视图控制器的实现。如果你重写此方法,你必须在你的实现中调用super。更多详细信息请参见http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIViewController_Class/Reference/Reference.html

0

请查看来自 Apple 的教程 Managing Appearance Updates for Children

这意味着您需要自己处理外观通知。

[self.child beginAppearanceTransition: YES animated: animated];
//
[self.child endAppearanceTransition];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接