【问题标题】:Rotation in iOS 6 with custom container view controller使用自定义容器视图控制器在 iOS 6 中旋转
【发布时间】:2012-09-29 19:21:30
【问题描述】:

我的应用程序中有一个自定义容器视图控制器,但无法在 iOS 6 中实现与在 iOS 5 中相同的旋转行为。

容器(称为 containerVC)包含两个视图控制器,一个应该保持纵向(portraitVC),一个可以旋转到横向(rotatingVC)。我使用分段控件在它们之间切换。

如果我打开 containerVC 最初显示的是 PortraitVC,然后将手机旋转到横向,portraitVC 不会正确旋转。但是,如果我切换到旋转VC,旋转到横向,然后在手机仍处于横向状态时切换到纵向VC,纵向VC 会错误地绘制横向。

在 iOS 5 中,portraitVC 始终保持纵向。

我在 containerVC 中有这段代码用于切换视图控制器:

- (IBAction)segmentChanged:(id)sender {
    UIViewController *toViewController = [self viewControllerForSegmentIndex:self.selectedSegmentIndex];
    [self addChildViewController:toViewController];

    UIViewController *fromViewController = self.selectedViewController;

    [self transitionFromViewController:self.selectedViewController
                      toViewController:toViewController
                              duration:0
                               options:0
                            animations:^{}
                            completion:^(BOOL finished) {
                                self.selectedViewController = toViewController;
                                [toViewController didMoveToParentViewController:self];
                                [fromViewController removeFromParentViewController];
                            }];
}

这个在containerVC中处理旋转:

- (NSUInteger)supportedInterfaceOrientations {
    UIInterfaceOrientationMask mask = UIInterfaceOrientationMaskPortrait;
    if ([self.selectedViewController respondsToSelector:@selector(supportedInterfaceOrientations)] ) {
        mask = [self.selectedViewController supportedInterfaceOrientations];
    }
    return mask;
}

这在portraitVC中:

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

这在rotatingVC中:

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAll;
}

当我在选择rotatingVC 后选择portraitVC 时,容器VC 或portraitVC 上没有调用任何旋转方法或回调。外观方法被调用,并且持有 tableview 的 PortraitVC 在 tableview 回调中获取具有横向几何图形的 UITableViewCells。

如果我们必须让 PortraitVC 支持横向,这不是世界末日,但如果可能的话,为了与应用程序的其他部分保持一致,最好不要这样做。似乎应该有一种方法可以让它工作,因为当您对它们进行子类化并覆盖supportedInterfaceOrientations时,内置的容器VC可以正常工作。

【问题讨论】:

  • 从未找到解决此问题的好方法。相反,我现在支持 VC 中的横向,它应该只是纵向。我还向 Apple 提交了一个错误(雷达编号:12394782),因为当调用 transitionFromViewController: 时,似乎应该在容器 vc 上调用 supportInterfaceOrientations。

标签: ios ios6


【解决方案1】:

我今天遇到了一个非常相似的问题,并通过替换根控制器解决了它 - 我创建了新的视图控制器并将其分配给应用程序窗口的根视图控制器。这个新控制器仅支持横向。原始根控制器支持这两种模式。 所以不是 [self addChild... 我旋转设备并替换根视图控制器。

UIInterfaceOrientation current = [[UIDevice currentDevice] orientation];
if ( UIInterfaceOrientationIsPortrait(current)) {
    [UIView beginAnimations:@"InterfaceOrientation" context:nil];
    [UIView setAnimationDuration:[application statusBarOrientationAnimationDuration]];
    [[[application delegate] window] setRootViewController:controller];
    [UIView commitAnimations];
    [application setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:YES];
}
else {
    [[[application delegate] window] setRootViewController:controller];
}

在 iOS 6 之前我没有使用控制器中的三种方法,并且您在问题中没有提到: viewWillLayoutSubviews、willRotateToInterfaceOrientation 和 didRotateFromInterfaceOrientation。例如,在 willRotateToInterfaceOrientation 中我保存当前方向,在 viewWillLayoutSubviews 中我重新排列所有子视图。

如果你创建一个子视图控制器。它使用关于设备方向的父视图控制器设置——我想是的。

我会很高兴更好地了解情况并找到更好的解决方案。

【讨论】:

  • 您好,感谢您的回复!这是一种有趣的方法,可以根据需要替换窗口的 rootViewController,但我认为这对我来说不是一个好方法,因为 containerVC 已经被推送到表示堆栈深处的好几层上。很高兴你的代码能正常工作!
【解决方案2】:

我注意到我有同样的问题,只是措辞不同:iOS 6: How do I restrict some views to portrait and allow others to rotate?

我只是对我自己的问题给出了部分答案,这可能会有所帮助。我喜欢您让容器视图向其子级询问支持的方向的想法;也许在 iOS shouldAutorotateToInterfaceOrientation: 是一个更好的主意。

【讨论】:

  • 感谢您的来信!我认为我的问题有点不同,因为它特定于 iOS 5 中引入的自定义容器视图控制器。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多