【问题标题】:Modal View Controller force Landscape orientation in iOS 6iOS 6 中的模态视图控制器强制横向方向
【发布时间】:2012-08-21 18:04:39
【问题描述】:

我有一个以纵向模式呈现的 UITabBarController。在其中一个选项卡上,我有一个以模态方式显示 UIViewController 的按钮(一个简单的故事板 segue 执行操作)。

我希望这个模态视图以横向模式显示,但我无法让它自动转动。

我在模态视图控制器中有这个

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

我已将 LandscapeLeft 添加到 .plist 支持的方向(尽管这也允许我不希望旋转 TabBar)

我也将它添加到模态视图的 ViewDidLoad 中

[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationLandscapeLeft;

但我就是不能让它自己旋转。

谢谢

编辑----

似乎 shouldAutoRotate 甚至没有被调用!

另外,我正在尝试检测方向,下面的代码总是显示 2,无论方向如何!

if ([[UIDevice currentDevice] orientation] == UIInterfaceOrientationPortrait) {
        NSLog(@"1");
        self.hostView.frame = CGRectMake(0, 60, 200, 255);
    } else {
        NSLog(@"2");
        self.hostView.frame = CGRectMake(0, 45, 480, 255);
    }

编辑 2 ---

我的错。我想我应该提到我使用的是 iOS 6。在 iOS 5 上显示会自动旋转。 shouldAutorotateToInterfaceOrientation 已弃用,因此我需要阅读新方法。

【问题讨论】:

  • 从我以前的经验来看,没有办法强制 viewController 出现在特定的方向。您可以选择是否允许更改方向,但不能强制更改。这对我来说是一个无赖...
  • 真的有点傻!我有一个只适合横向的图表。所以你说苹果想让我把它搞砸了,直到用户转动他的设备。这听起来不对,虽然我还没有找到解决方案,所以你可能是对的。

标签: ios xcode cocoa-touch modalviewcontroller uiinterfaceorientation


【解决方案1】:

iOS 6.0 发行说明
“更多的责任正在转移到应用程序和应用程序代理身上。现在,iOS 容器(例如 UINavigationController)不会咨询它们的子容器来确定它们是否应该自动旋转。” em>

任何Container(如UINavigationController)下UIViewController的-(BOOL)shouldAutorotate都不会被IOS6调用。

尝试使用 Category 将方法添加到现有 UINavigationController 类的解决方案。

shouldAutorotate方法中,可以在self.viewControllers的每个视图控制器中调用shouldAutorotate方法。实际上,您可以传递给您的子视图控制器。

【讨论】:

    【解决方案2】:

    如果你想在 iOS6 中强制旋转,你的 rootviewController 应该实现这些方法:

    - (NSUInteger)supportedInterfaceOrientations {
        return UIInterfaceOrientationMaskLandscape;
    }
    
    - (BOOL)shouldAutorotate {
        return YES;
    }
    

    【讨论】:

      【解决方案3】:

      在 iOS 6 中,这种方法似乎有助于在 iOS 6 上强制指定特定方向。

      - (NSUInteger)supportedInterfaceOrientations {
          return UIInterfaceOrientationMaskLandscape;
      }
      

      我仍在努力让它发挥作用,并将根据任何发现进行更新。

      【讨论】:

      • 谢谢,它在我的情况下与方法一起工作 -(BOOL)shouldAutorotate{ return NO;}
      【解决方案4】:

      我在我的应用程序中执行此操作,并通过 shouldAutoRotateToInterfaceOrientation 执行此操作,与您略有不同:

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

      对于 iOS 6,您还必须设置 window.rootViewController = {您的根视图控制器};在您的应用委托中。我希望这是你的问题

      【讨论】:

      • 这仍然不会强制定向。我已将此添加到我的 AppDelegate self.window.rootViewController = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"mainTab"];
      • 好吧,真可惜,我没有在我的应用程序中使用故事板,但我认为这不会导致这个问题。我怀疑无论如何都会自动为您设置 rootViewController 属性。记录对 shouldAutorotate 的调用以查看它是否被调用会很有趣
      • 不,根本没有调用 shouldAutorotate!
      • 糟糕。 shouldAutorotateToInterfaceOrientation 在 iOS 6 中已弃用。我想我应该提到我正在使用它。
      猜你喜欢
      • 1970-01-01
      • 2013-08-26
      • 1970-01-01
      • 2012-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-27
      • 1970-01-01
      相关资源
      最近更新 更多