禁用导航控制器中View Controller的旋转 [英] Disable rotation for View Controller in Navigation Controller

查看:157
本文介绍了禁用导航控制器中View Controller的旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,这不是重复。我在SO上查看了与此相关的所有问题,但这些问题对我来说都不起作用。希望这只是因为我是iOS开发的新手,但我怀疑在我的情况下这是不可能的。我附加了一个带有视图控制器的图片,我想要禁用旋转。

First of all, this isn't a duplicate. I've looked at all of the questions related to this on SO and none of them work for me. Hopefully it's just because I'm new to iOS development but I suspect this isn't possible in my case. I've attached a picture with the view controller circled that I want to disable rotation for.

我已经尝试过:对要禁用旋转的视图控制器进行子类化通过将其设置为NO来使用 shouldAutoRotate 方法。显然这不起作用,因为导航控制器决定了它的视图控制器是否可以旋转。因此,我将 UINavigationController 子类化,并在故事板中使用此类而不是默认的导航控制器。在这个课程中,我将 shouldAutoRotate 设置为NO。它仍然无法正常工作。真的不知道我做错了什么。

I've already tried: Subclassing the view controller that I want to disable rotation for and using the shouldAutoRotate method by setting it to NO. Apparently this doesn't work because it's the navigation controller that dictates whether its view controllers can rotate. So, I subclassed UINavigationController and used this class in storyboard instead of the default navigation controller. In this class I've set shouldAutoRotate to NO. It still doesn't work. Don't really know what I'm doing wrong.

当我使用我的视图控制器类扩展根视图控制器时带有 shouldAutoRotate 设置为NO,它禁用旋转...对于整个应用程序。这不是我想要的。我只希望为图片中圈出的视图控制器禁用旋转。

When I extend the root view controller with my view controller class with shouldAutoRotate set to NO, it disables rotation...for the whole app. This is not what I want. I only want the rotation to be disabled for the view controller circled in the picture.

提前致谢!

推荐答案

添加你的AppDelegate.h

Add your AppDelegate.h

@property (nonatomic , assign) bool blockRotation;

AppDelegate.m

AppDelegate.m

-(NSUInteger)application:(UIApplication *)application       supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if (self.blockRotation) {
    return UIInterfaceOrientationMaskPortrait;
}
return UIInterfaceOrientationMaskAll;
}

在视图中要禁用旋转的控制器

In view Controller that you want to disable rotation

- (void)viewDidLoad
{
 [super viewDidLoad];
    AppDelegate* shared=[UIApplication sharedApplication].delegate;
    shared.blockRotation=YES;
}

-(void)viewWillDisappear:(BOOL)animated{
  AppDelegate* shared=[UIApplication sharedApplication].delegate;
  shared.blockRotation=NO;

  }

这篇关于禁用导航控制器中View Controller的旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆