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

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

问题描述

首先,这不是重复的.我已经在 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;

  }

Swift 4.2 及更高版本:

在您的 AppDelegate.swift 中:

In your AppDelegate.swift:

var blockRotation = false

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    if blockRotation {
        return .portrait
    }
    return .all
}

在您的视图控制器中:

override func viewDidLoad() {
    super.viewDidLoad()
    AppDelegate.shared.blockRotation = true
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    AppDelegate.shared.blockRotation = false
}

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

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