4

I know this question was already asked in here, here and here. I tried all possible ways but wasn't successful.

I'm trying to develop an application in Swift (iOS 8), in which only one view controller can be viewed in all orientations others in only portrait.

For this, I have enabled only Portrait mode in Target->General->Device Orientation (because I want the application to operate only in portrait mode except one view controller).

I have added below code to the view controller that I want to operate in all orientations:

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

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

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

But couldn't rotate it in landscape mode.

Community
  • 1
  • 1
Srujan Simha
  • 3,637
  • 8
  • 42
  • 59

2 Answers2

3

Do the following:

  1. enable the desired orientations in the General Info Screen
  2. in each ViewController, override the autorotation function with either true or false
    (true for the one that should rotate, false for the others):

    override var shouldAutorotate: Bool {
        return true
    }
    

Hope that helps :)

LinusGeffarth
  • 27,197
  • 29
  • 120
  • 174
  • `override` autorotation inside `viewWillAppear`? How is that possible? – Srujan Simha Apr 15 '15 at 17:05
  • Thaaaaat is right. Well, let me fix that. Thanks for pointing that out – LinusGeffarth Apr 15 '15 at 17:07
  • I'm not at my Mac right now so I can't check, is there in the into.plist something like `ViewController-Based Orientation`? (Like for the StatusBar) If so, then make sure to set that to **true**. – LinusGeffarth Apr 15 '15 at 17:28
  • I got it worked! You're right, it's in the `info.plist`, but not `ViewController-Based Orientation` it's `Supported interface orientations` both for iPhone and iPad. This's not exactly what I wanted but this solves my problem partially. – Srujan Simha Apr 15 '15 at 18:10
0

Try this in your perticular viewController which you want in all orientation, in viewWillAppear:

 let value = UIInterfaceOrientationMask.All.rawValue

 UIDevice.currentDevice().setValue(value, forKey: "orientation") 

and don't forget to allow orientations in Target->General->Device Orientation

Give it a try

iAnurag
  • 9,286
  • 3
  • 31
  • 48