Disabling rotation for current screen?

我只是一个虾纸丫 提交于 2019-11-28 11:14:28

问题


Is it possible to enable/disable rotation parameter for current screen or this property is for all application?


回答1:


Sure:

- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    //Choose your available orientation, you can also support more tipe using the symbol |
    //e.g. return (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight)
    return (UIInterfaceOrientationMaskPortrait);
}



回答2:


If you have multiple ViewControllers within a NavigationController, and you wish to disable rotation in one of them only, you need to set and control rotation within ApplicationDelegate. Here is how to do it in Swift...

In AppDelegate.swift:

class AppDelegate: UIResponder, UIApplicationDelegate {


var blockRotation: Bool = false

func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int {

    if (self.blockRotation) {
        println("supportedInterfaceOrientations - PORTRAIT")
        return Int(UIInterfaceOrientationMask.Portrait.rawValue)
    } else {
        println("supportedInterfaceOrientations - ALL")
        return Int(UIInterfaceOrientationMask.All.rawValue)
    }
}

In the ViewController that you want to block rotation, add UIApplicationDelegate to your class...

class LoginViewController: UIViewController, UITextFieldDelegate, UIApplicationDelegate {

and then create a reference to the AppDelegate...

var appDelegate = UIApplication.sharedApplication().delegate as AppDelegate

In viewDidLoad, set appDelegate.blockRotation = true:

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    appDelegate.blockRotation = true

}

In viewWillAppear, set the orientation to force the device to the chosen orientation (Portrait in this example):

override func viewWillAppear(animated: Bool) {

    let value = UIInterfaceOrientation.Portrait.rawValue
    UIDevice.currentDevice().setValue(value, forKey: "orientation")

}

Then in viewWillDisappear, or in prepareForSegue, set appDelegate.blockRotation = false:

override func viewWillDisappear(animated: Bool) {
    appDelegate.blockRotation = false
}

This will block rotation in the one view controller within a Navigation Controller that contains multiple ViewControllers. Hope this helps.



来源:https://stackoverflow.com/questions/23468758/disabling-rotation-for-current-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!