如何仅为一个视图控制器锁定方向? [英] How to lock orientation just for one view controller?

查看:108
本文介绍了如何仅为一个视图控制器锁定方向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以仅在一个控制器中设置纵向方向和锁定方向吗?

Can I set a portrait orientation and lock orientation just in one controller?

我确实尝试过:

override func supportedInterfaceOrientations() -> Int {
    return Int(UIInterfaceOrientationMask.Portrait.rawValue)
}

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

但这对我没有帮助.我还必须添加其他内容吗?

but it doesn't help me. Must I to add anything else?

推荐答案

此代码应该有效:

    override func supportedInterfaceOrientations() -> Int {
        return Int(UIInterfaceOrientationMask.Portrait.rawValue)
    }

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

    override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
        return UIInterfaceOrientation.Portrait
    }

如果它现在正在为您工作,那么我想您的控制器在另一个控制器中(UINavigationControllerUITabBarControllerUISplitViewController).在这种情况下,您需要在该父控制器中使用此代码.

If it is now working for you, then I suppose your controller is in some another controller(UINavigationController, UITabBarController, UISplitViewController). In this case you need to use this code in that parent controller.

如果导航控制器包含多个视图控制器,并且只需要禁用其中的某些方向,则需要继承UINavigationController类并在其中编写类似内容:

If your navigation controller contain more than one view controller, and you need to disable orientation only for some of them, then you need to inherit UINavigationController class and write there something like:

class NavigationController: UINavigationController {

    var shouldRotate: Bool = true

    override func supportedInterfaceOrientations() -> Int {
        return shouldRotate ? Int(UIInterfaceOrientationMask.Portrait.rawValue) : Int(UIInterfaceOrientationMask.All.rawValue)
    }

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

然后在控制器中需要禁用方向,可以为导航控制器禁用它:

Then in controller that you need to disable orientation you can disable it for your navigation controller:

class ViewController: UIViewController {

    var lastControllerRotationStatus: Bool?

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)

        if let navigationController = self.navigationController as? NavigationController {
            lastControllerRotationStatus = navigationController.shouldRotate
            navigationController.shouldRotate = false
        }
    }

    override func viewDidDisappear(animated: Bool) {
        super.viewDidDisappear(animated)

        if let navigationController = self.navigationController as? NavigationController {
            navigationController.shouldRotate = lastControllerRotationStatus ?? true
        }
    }
}

但是请记住,在将控制器推出导航控制器之后,您需要恢复旧的旋转状态.在此示例中,我将保存旋转状态,然后再进行更改,并在控制器消失后进行恢复.您也可以使用其他方法.例如,您可以重载UINavigationController方法popViewController,然后将shouldRotate设置为false.

But remember, that you need to restore old rotation status after your controller will be pushed out navigation controller. In this example I'm saving rotation status before changing it, and restoring it after controller disappeared. Also you can use some other approach. For example you can overload UINavigationController method popViewController, and there set shouldRotate to false.

与通过控制器进行变量设置的方法相同,可用于通过AppDelegate方法application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow) -> Int

The same approach with variable setting from controller you can use for controlling rotation from AppDelegate method application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow) -> Int

然后,您无需从导航控制器继承.

Then you do not need to inherit from navigation controller.

您的AppDelegate类代码如下所示:

class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    var shouldRotate = true
    func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int {

        return shouldRotate ? Int(UIInterfaceOrientationMask.All.rawValue) : Int(UIInterfaceOrientationMask.Portrait.rawValue)

    }

}

您的控制器代码将如下所示:

And your controller code will look like:

class ViewController: UIViewController {

    var lastControllerRotationStatus: Bool?

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)

        if let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate {
            lastControllerRotationStatus = appDelegate.shouldRotate
            appDelegate.shouldRotate = false
        }
    }

    override func viewDidDisappear(animated: Bool) {
        super.viewDidDisappear(animated)

        if let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate {
            appDelegate.shouldRotate = lastControllerRotationStatus ?? true
        }
    }
}

这篇关于如何仅为一个视图控制器锁定方向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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