iOS版。如何在每个UIViewController上启用和禁用旋转? [英] iOS. How to enable and disable rotation on each UIViewController?

查看:997
本文介绍了iOS版。如何在每个UIViewController上启用和禁用旋转?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UIViewController,我想在不同场景中禁用或启用屏幕旋转

I have an UIViewController, I want to disable or enable rotation of the screen in different scenarios

示例:

if flag {
   rotateDevice = false
}
else {
   rotateDevice = true
}

我该怎么做?

推荐答案

我有答案。
On AppDelegate ,如果您旋转设备,请按viewcontroller等。此函数始终调用

I have the answer. On AppDelegate, if you rotate device, push viewcontroller, etc. This function always call

更新for swift 3/4

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask
{
    return self.restrictRotation
}

self.restrictRotation 是一个自定义参数。

如何使用:

在Appdelegate中:

 var restrictRotation:UIInterfaceOrientationMask = .portrait

在ViewController中:

调用方法ViewDidLoad或viewWillAppear时。我们将改变如下:

When method ViewDidLoad or viewWillAppear is called. We will change like this:

(UIApplication.shared.delegate as! AppDelegate).restrictRotation = .all 

然后将调用AppDelegate上的此方法。

and then this method on AppDelegate will be called.

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask

这篇关于iOS版。如何在每个UIViewController上启用和禁用旋转?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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