Suvrat Apte
Suvrat Apte

Reputation: 171

shouldAutorotate -> false, still simulator rotates

I'm making an app which supports landscape orientations only when playing a video. Otherwise, all the scenes support only portrait orientation. I've checked portrait, landscape left and right in project settings. I've written the following code in the ViewControllers where I want to restrict to only portrait.

override func viewWillAppear(animated: Bool) {
    let value = UIInterfaceOrientation.Portrait.rawValue
    UIDevice.currentDevice().setValue(value, forKey: "orientation")
}

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

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

Still, when I press (Command + right or left arrow), the simulator rotates. I don't have a device, so I have to test it in simulator.

Please help! Thank you!

Upvotes: 3

Views: 1223

Answers (2)

Dave
Dave

Reputation: 12216

Two things to check. First, check your info.plist file and make sure that you have portrait deleted for both iPhone and iPad. I was having the same issue and it was because I hadn't deleted the iPad "Portrait" options. See here:

enter image description here

Second, the below code can help. However, it may not work if you have a navigation controller.

override var shouldAutorotate: Bool {
    return true
}

Upvotes: 0

SeNeO
SeNeO

Reputation: 417

It's the parent navigation controller that decide if its content should rotate or note.

You will need to override UINavigationController and had something like this

override func shouldAutorotate() -> Bool {
    return self.topViewController.shouldAutorotae()
}

override func supportedInterfaceOrientations() -> Int {
    return self.topViewController.shouldAutorotae()
}

Upvotes: 0

Related Questions