【问题标题】:Force Landscape iOS 6强制横向 iOS 6
【发布时间】:2013-04-21 15:44:04
【问题描述】:

我希望我的应用中的一个视图具有横向方向。我已经设法让视图在手动旋转时保持在横向,但如果设备已经是纵向的,它会保持纵向,无论其支持的方向如何(使用supportedInterfaceOrientations 方法设置)。有没有办法让视图自动旋转?我试过了:

[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];

但这不起作用。

任何建议将不胜感激!

【问题讨论】:

标签: iphone ios objective-c rotation


【解决方案1】:

做到这一点的一种方法是覆盖preferredInterfaceOrientationForPresentation,但要调用它,必须呈现(如在模态中)而不是像here提到的那样推送:

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    NSLog(@" preferred called");
    return UIInterfaceOrientationPortrait;
}

为了在 UINavigationController 中展示您的 viewController,请使用:

UINavigationController *presentedNavController = [[UINavigationController alloc] initWithRootViewController:protraitViewController];

[self presentViewController:presentedNavController animated:YES completion:nil];

要使 UINavigationController 尊重您当前 viewController 的方向偏好,请使用这个简单的category 而不是子类化。

此外,Apple 文档的 part 是一本很好的读物,可以更好地理解 iOS 方向处理。

【讨论】:

【解决方案2】:

在 UIViewController 中为您的横向视图定义以下内容:

- (UIInterfaceOrientation) supportedInterfaceOrientations
{ return UIInterfaceOrientationLandscapeLeft; } // or right or both

这应该会阻止您的视图以纵向显示。来自 iOS 文档:

声明首选的演示方向 当一个视图控制器 全屏呈现以显示其内容,有时是内容 以特定方向观看时,效果最佳。如果 内容只能以该方向显示,那么您只需 将其作为您的唯一方向返回 supportedInterfaceOrientations 方法。

【讨论】:

  • 我已经在我的代码中使用了:return UIInterfaceOrientationLandscapeRight| UIInterfaceOrientationLandscapeLeft;。对不起,我忘了把它包括在我的问题中
  • 这永远不会调用。请检查。