无法仅在 xcode 4 中将屏幕方向锁定为纵向?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6963174/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unable to lock screen orientation to portrait only in xcode 4?
提问by Omar
In xcode 4 I am unable to lock the screen orientation to only portrait even though I have portrait selected only. How to I do this programmatically?
在 xcode 4 中,即使我只选择了纵向,我也无法将屏幕方向锁定为仅纵向。如何以编程方式执行此操作?
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
this code works in a new blank project
此代码适用于一个新的空白项目
回答by James Harpe
The accepted answer did not work for me either. This did:
接受的答案对我也不起作用。这做到了:
In the properties file for your app (YOURAPPNAME-Info.plist), located in the "supporting files" group, there is an array called "Supported interface orientations". Remove both landscape values from the array and your app will be locked in portrait orientation.
在您的应用程序的属性文件 (YOURAPPNAME-Info.plist) 中,位于“支持文件”组中,有一个名为“支持的界面方向”的数组。从数组中删除两个横向值,您的应用程序将被锁定为纵向。
回答by PengOne
For each view controller (or just the parent), implement -shouldAutorotateToInterfaceOrientation:
Set the default orientation to portrait and then do:
对于每个视图控制器(或只是父视图),实现-shouldAutorotateToInterfaceOrientation:
将默认方向设置为纵向,然后执行:
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return NO;
}
Alternately, you can do:
或者,您可以执行以下操作:
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return interfaceOrientation == UIInterfaceOrientationPortrait;
}
which will allow a rotation to portrait but not away from it.
这将允许旋转到纵向但不能远离它。