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

Dar*_*ren 8 xcode cocoa-touch uiinterfaceorientation modalviewcontroller ios

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

我希望这个模态视图以横向模式显示,但我不能让它自动转动.

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

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}
Run Code Online (Sandbox Code Playgroud)

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

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

[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationLandscapeLeft;
Run Code Online (Sandbox Code Playgroud)

但我不能让它自己旋转.

谢谢

编辑----

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

此外,我正在尝试检测方向,下面的代码总是显示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);
    }
Run Code Online (Sandbox Code Playgroud)

编辑2 ---

我的错.我想我应该提到我正在使用iOS 6.显示器在iOS 5上自动旋转.不推荐使用shouldAutorotateToInterfaceOrientation,所以我需要阅读新方法.

小智 7

iOS 6.0发行说明
"更多的责任转移到应用程序和应用程序委托.现在,iOS容器(例如UINavigationController)不会咨询他们的孩子以确定他们是否应该自动旋转."

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

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

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


mih*_*ham 6

如果要在iOS6中强制旋转,rootviewController应该实现以下方法:

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscape;
}

- (BOOL)shouldAutorotate {
    return YES;
}
Run Code Online (Sandbox Code Playgroud)