【问题标题】:How to force my iPhone application to stay in a specific orientation如何强制我的 iPhone 应用程序保持在特定方向
【发布时间】:2012-06-23 10:36:33
【问题描述】:

是的,我知道这个问题已经被问过很多次了,但我找不到任何可以进一步帮助我的东西。

使用带有 3 个视图控制器的导航控制器,我需要保留以前屏幕中的数据,因此我不使用 segues,而是像这样:

// When button is pressed
- (IBAction)changeView:(id)sender {
NSLog(@"Skipped connection screen");
ViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"SecondView"];

[self.navigationController pushViewController:vc animated:YES];

} 其中 SecondView 是应该出现的视图控制器的标识符。因为我只希望旋转在水平右侧,所以我在我拥有的每个 .m 文件的顶部添加了这段代码的 sn-p 代码:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{

return (self.interfaceOrientation == UIInterfaceOrientationLandscapeRight);  
}

在我的 project-Info.plist 中,我添加了 Initial interface orientation = Landscape (right home button,在我的项目设置中,我只添加了对这个方向的支持。

问题是,当我在 iPhone 上运行时,如果我以任何一种方式转动手机,方向都会从横向改变。当我试图把它转回来时,它就不会了。我想确保这个应用程序永远能够从横向旋转。

有什么建议吗?非常感谢您提前。

【问题讨论】:

  • 请看我下面的答案,如果你有任何问题,请告诉我。

标签: iphone ios xcode uiviewcontroller uinavigationcontroller


【解决方案1】:

我认为如果您将以下密钥添加到您的 .plist 文件中,那么它将被修复。

"Supported interface orientations" 此键的值将是 "Landscape (right home button)" 或您想要的任何值,因此您的应用程序将仅支持指定的方向。

还将此代码添加到每个视图控制器中。

- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)toInterfaceOrientation{
return (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight);  
}

【讨论】:

  • 这适用于应用程序范围内的一劳永逸的固定方向。如果您有大量不同的视图控制器,那么这一行就很简单了。
  • 我认为我的项目设置 -> 摘要 -> 支持的设备方向已经解决了这个问题,但似乎没有。谢谢
【解决方案2】:

您应该在“返回”代码中使用该参数:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
   return (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight);  
}

【讨论】:

  • 非常感谢,成功了。您能解释一下为什么它不适用于我的代码吗?
  • @Wilhelmsen:您的代码无法正常工作,因为您使用的是 self.interfaceorientation。据我所知 self.interfaceorientation 将在 shouldAutorotate 执行后获得价值。实际上,在 ShouldAutorotate 中,您必须比较从系统接收到的参数值,并将其与您希望允许的方向进行比较。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-27
  • 2014-01-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多