在iOS7中禁用UINavigationController的旋转

时间:2014-02-20 20:57:47

标签: ios objective-c uiviewcontroller uinavigationcontroller rotation

我有UINavigationController创建UIViewController的实例,并将另一个UIViewController设置为root。然后我展示了导航控制器,一切正常。

用户旋转设备时出现问题。我的所有控制器(包括root和UINavigationController除外)都会实现shouldAutorotate并返回FALSE

不知何故,我的观点仍在转动。我们来到我的问题的中心。我像这样创建并呈现导航控制器:

AwesomeViewController *controller = [[AwesomeViewController alloc] init];

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:controller];

[self presentViewController:navController 
                   animated:YES 
                 completion:NULL];

你可以看到它非常直接。

  1. navController遗失了shouldAutorotate设置为FALSE,这就是为什么它在旋转,我是否正确?

  2. 如何以纵向方向锁定它?

  3. 可以在没有像这样荒谬的子类的情况下完成:

    @interface LockedRotationNavigationController : UINavigationController
    @end
    
    @implementation LockedRotationNavigationController
    - (BOOL)shouldAutorotate { return NO; }
    @end
    
  4. 我真正想要的是如何禁用UINavigationController 的轮换而不进行子类化?

    该属性为readonly,因此我不知道如何操作。

1 个答案:

答案 0 :(得分:2)

要在Info.plist展开 Supported interface orientations 中全局禁用轮播,并移除横向项,以使您的应用仅以纵向模式运行。

enter image description here

或者您可以在 Xcode 中选择支持的方向,如下所示:

enter image description here

您还可以通过在视图控制器中实施 shouldAutorotateToInterfaceOrientation 方法以编程方式锁定轮播。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return YES;
}