如何强制我的iPhone应用程序保持特定的方向

时间:2012-06-21 08:09:08

标签: iphone ios xcode uiviewcontroller uinavigationcontroller

是的,我知道很多次都会问这个问题,但我找不到任何可以帮助我的东西。

使用带有3个viewcontrollers的导航控制器,我需要保留以前屏幕的数据,所以我不使用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文件的顶部添加了这段代码:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{

return (self.interfaceOrientation == UIInterfaceOrientationLandscapeRight);  
}

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

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

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

2 个答案:

答案 0 :(得分:2)

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

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

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

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

答案 1 :(得分:1)

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

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
   return (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight);  
}
相关问题