user4809833
user4809833

Reputation:

How to unlock orientation just for one controller?

How can I unlock orientation just for one controller? I allowed just one - portrait - mode for whole my application, but just in one I need a landscape mode. For this I tried the next snippet:

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

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

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

but it doesn't help me. Are there any other solutions?

Upvotes: 2

Views: 682

Answers (2)

Hamza Ansari
Hamza Ansari

Reputation: 3074

In Appdelegate define a var :

import UIKit
var isViewAppeared = false

now in Appdelegate:

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

Now in ViewController where you want landscape:

  override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    isViewAppeared = true

  }

  override func viewWillDisappear(animated: Bool) {
    super.viewWillDisappear(animated)
    isViewAppeared = false

    UIView.animateWithDuration(0.4, animations: { () -> Void in
      let value = UIInterfaceOrientation.Portrait.rawValue
      UIDevice.currentDevice().setValue(value, forKey: "orientation")
    })

  }

Upvotes: 2

iAnurag
iAnurag

Reputation: 9356

Do the following:

enable the desired orientations in the General Info Screen

in each ViewController.swift

  • override the autorotation function with either true or false (true for the one that should rotate, false for the others)

Hope that helps :)

Upvotes: 0

Related Questions