5

因此,像许多其他人一样,我遇到了一个问题,即只有一个或两个视图控制器支持纵向和横向界面方向,否则只能在纵向应用程序中。在 iOS 6 之前一切正常,但突然自动旋转停止工作。多亏了这里的一些好问题,我能够通过让初始 navController 返回单个 topViewController 对 shouldAutorotate 的偏好来解决该问题:

    - (BOOL)shouldAutorotate
{
    return  self.topViewController.shouldAutorotate;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAll;
}

但是,我偶然发现了一个新问题。根 vc (viewController A) 不应该自动旋转并且应该只支持纵向。导航堆栈中的 ViewController B 支持纵向和横向。如果我在viewController B中,并且在横向中,然后触摸“返回”以将视图弹出回viewController A ... vc A加载横向,它甚至不应该支持,并且不会旋转回纵向,因为vc A 的 shouldAutorotate 设置为 NO...

任何有关如何处理此问题的想法将不胜感激。我最初的想法是用手动方法覆盖vc B的“后退”按钮,如果视图处于横向状态,首先强制旋转回纵向......然后将视图控制器弹出回vc A......但我不知道如何以编程方式强制旋转。有任何想法吗?

以下是vc A中的接口方法:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return NO;
}

-(BOOL)shouldAutorotate
{
    return NO;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationPortrait;
}

这是它们在 vc B 中的内容:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

-(BOOL)shouldAutorotate
{
    return YES;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationPortrait;
}
4

2 回答 2

1

在 vcA 集中

-(BOOL)shouldAutorotate
{
  return YES;
}

但保持

-(NSUInteger)supportedInterfaceOrientations
{
  return UIInterfaceOrientationPortrait;
}

然后,当您从 vcB 返回时,视图将旋转回(仅)支持的方向

于 2012-11-07T11:35:24.937 回答
0

问题是所有容器视图控制器(选项卡栏控制器、导航控制器等)都支持您在 plist 文件中提供的所有界面方向。当系统要求支持的界面方向时,根视图控制器的设置和方法实现会覆盖它的子视图。

在这种情况下,导航控制器支持横向和纵向,当 B 视图控制器弹出时,尽管系统要求 A 的界面方向,它也会询问它的根视图控制器,这将是“赢家”,因为导航控制器支持横向,尽管 A 仅支持纵向,但它仍保持横向。

一种解决方案是,您将根视图控制器子类化并根据需要动态更改其旋转方法。当只需要 portait 时,root 的实现应该只返回 portait 并且当两个方向都可用时,那么你的 root 应该返回两者。

于 2013-04-12T19:43:31.977 回答