覆盖应用程序方向设置 [英] Override App Orientation Setting

查看:236
本文介绍了覆盖应用程序方向设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,仅在其目标设置中将方向设置为纵向":

I have an app where I set the orientation to be Portrait only in it's target settings:

在一个特定的视图控制器中,我想覆盖此设置,因此当设备旋转时,自动布局将更新视图.我尝试了这些方法,但均未成功:

In one particular view controller I'd like to override this setting so auto layout will update views when a device is rotated. I've tried these methods with no success:

override func shouldAutorotate() -> Bool {
    return true
}

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    return UIInterfaceOrientationMask.AllButUpsideDown
}

推荐答案

您所需的VC中的代码将不起作用. 我通过在AppDelegate中添加以下代码来管理此问题

Your code in the desired VC will not work. I've managed this by adding the following code in the AppDelegate

var autoRotation: Bool = false

func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
    return autoRotation ? .AllButUpsideDown : .Portrait
  }

然后您可以创建一个帮助器类并添加此方法:

And then you can make an helper class and add this method:

class func setAutoRotation(value: Bool) {
if let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate {
   appDelegate.autoRotation = value
}

}

最后,在所需的VC中,可以在didLoad和 在willDissapear上设置为setAutoRotation(false).

Finally in your desired VC, you can call setAutoRotation(true) on didLoad and setAutoRotation(false) on willDissapear.

这也可以通过子类化UINavigationController来实现.您可以在此处此处找到答案. 希望对您有帮助

This can also achieved by subclassing the UINavigationController. You can find the answer here. Hope it helps

这篇关于覆盖应用程序方向设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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