Disabling rotation for current screen?

后端 未结 2 1556
情书的邮戳
情书的邮戳 2020-12-22 01:14

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

相关标签:
2条回答
  • 2020-12-22 01:43

    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);
    }
    
    0 讨论(0)
  • 2020-12-22 01:49

    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.

    0 讨论(0)
提交回复
热议问题