模态视图控制器强制iOS 6中的横向方向

时间:2012-08-21 18:04:40

标签: ios xcode cocoa-touch modalviewcontroller uiinterfaceorientation

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

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

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

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

我已将landscapeLeft添加到.plist支持的方向(虽然这也允许TabBar旋转,我不想要)

我还将其添加到模态视图的ViewDidLoad

[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationLandscapeLeft;

但我无法让它自行旋转。

由于

编辑----

似乎应该甚至没有调用自动旋转!

另外,我正在尝试检测方向,下面的代码总是显示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已被弃用,因此我需要阅读新方法。

4 个答案:

答案 0 :(得分:7)

  

iOS 6.0发行说明
  “更多的责任转移到app和app delegate。现在,iOS容器(例如UINavigationController)不会咨询他们的孩子以确定他们是否应该自动旋转。”

不会从IOS6调用任何Container(如UINavigationController)下的UIViewController的

-(BOOL)shouldAutorotate

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

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

答案 1 :(得分:6)

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

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscape;
}

- (BOOL)shouldAutorotate {
    return YES;
}

答案 2 :(得分:1)

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

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscape;
}

我仍然试图让它发挥作用,并会根据任何调查结果进行更新。

答案 3 :(得分:0)

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

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

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

相关问题