如何锁定iOS中的屏幕才能使其只有纵向方向?

我创建了一个包含许多视图的应用程序,我希望其中一些仅以纵向方式显示。 我在.m文件中编写了这个代码:

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return NO; } 

我还需要做别的吗? 可能在.h文件中?

对我来说最干净的解决方案:

转到Info.plist文件,对于“支持的界面方向”,删除“纵向(底部主页按钮)”以外的所有值。

您只需要在该方法中返回BOOL。 如果你只想要肖像模式,那意味着:

 -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationPortrait) ; } 

如果将肖像上下颠倒(当纵向旋转设备180度时),则该方法将如下所示:

 -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationPortrait) || (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown); } 

最后一个条件可以替换为对UIDeviceOrientationIsPortrait(interfaceOrientation)的调用,它进行相同的比较(参见: http : //developer.apple.com/library/ios/#documentation/uikit/reference/UIKitFunctionReference/Reference/reference。 HTML )

LE:如果这不起作用,你可以尝试使用跟随’hack’:尝试弹出并再次推送视图(如果你正在使用NavigationController)。 您可以使用popViewControllerAnimatedpushViewController:animated:方法强制控制器重新查询所需的方向:)来源: http : //developer.apple.com/library/ios/#documentation/uikit/reference/UINavigationController_Class/Reference/ Reference.html )

你必须为你支持的方向(肖像)返回YES ,而不是一切都不是NO 。 还要确保在项目的目标设置中,您只能将纵向模式检查为支持。

将以下方法添加到viewcontroller。 这将确保仅支持例如portarait模式。

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return UIInterfaceOrientationIsPortrait(interfaceOrientation); } -(NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait; } 

尝试这个..

  -(BOOL)shouldAutorotate { return NO; } -(NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationPortrait); }