30

I locked the app orientation to portrait from the general app setting like this

portrait

but still when i run my application on ipad, it rotates!?

so where should i lock the orientation ?

why apple added this feature to settings, while it is not working !?

UPDATE:

I tried to implement this viewController as the general for all the viewController as well.

import Foundation
import UIKit

class GeneralViewController: UIViewController {

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

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


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

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

this pic taken from the main navigationController of my app.

Pic of answer 1

still did not work!

how on earth this thing works?

MBH
  • 16,271
  • 19
  • 99
  • 149

7 Answers7

115

You don't need extra code, I'm working on Xcode 7.0.1, I noticed that when I lock screen orientation to only portrait, it locks only in iPhones, to lock it in iPads also, you need to select iPad from Devices (instead of Universal) and check portrait and uncheck the others and then return Devices to Universal again, as I found that Universal only reflect in iPhones, it maybe some kind of bug in Xcode.

Mohammed Elrashidy
  • 1,820
  • 1
  • 14
  • 16
8

You can override your view controller property shouldAutorotate

override var shouldAutorotate: Bool {
    return false 
}

If your view controller is embedded in a Navigation controller you can create a custom Navigation controller:

class PortraitNavigationController: UINavigationController {

    override var shouldAutorotate: Bool {
        return false 
    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */
}
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
8

I also had same problem. What worked for me was -> Select iPad instead of Universal in Device Type -> Uncheck the other three options except Portrait -> Select Universal again. Done.

user6685376
  • 81
  • 1
  • 1
5

This would be a comment added to Mohammed Elrashidy solution but I don't have the reputation to make comments. I am using XCode 9.1 and Swift 3.

My app requires some graphing screens to autorotate but all the other screens to remain fixed. My issue was that the "iPad" device configuration in General > Deployment Info was configured inconsistently with the iPhone and Universal devices. I had "Portrait", "Landscape Left" and "Landscape Right" checked for iPhone and Universal but all of the orientations checked for iPad (including "Upside Down"). Simply unchecking "Upside Down" caused the iPad device to honor the shouldAutorotate and supportedInterfaceOrientations calls in my Swift logic.

My Info.plist now looks like the following and I can programmatically turn autorotation on or off for the iPad.

<key>UISupportedInterfaceOrientations</key>
<array>
    <string>UIInterfaceOrientationPortrait</string>
    <string>UIInterfaceOrientationLandscapeLeft</string>
    <string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
    <string>UIInterfaceOrientationPortrait</string>
    <string>UIInterfaceOrientationLandscapeLeft</string>
    <string>UIInterfaceOrientationLandscapeRight</string>
</array>
Dave Stevens
  • 106
  • 2
  • 6
  • Perfect solution, thanks! Setting this in the plist is most likely the better alternative – JohnV Aug 18 '18 at 02:47
3

I just wanted to add something else you need to do, after fixed the issue following the above solution:

In XCode, make sure you also check the "Requires Full Screen" checkbox under General > Targets.

Reference: iPad Multitasking support requires these orientations

RainCast
  • 4,134
  • 7
  • 33
  • 47
  • Under-rated comment, after trying 10+ answers, this is the one that finally worked. Also required adding code to the ViewController. XCode 12. – GregT Aug 19 '21 at 15:34
1

Since the XCode changed a lot in these 5 years, I found an easy way to handle it when I ran into the same issue that even I configured the Device Orientation to only allow "Portrait", the restriction is not working in iPad device.

So in newly created project, need go to Targets->Info->Custom iOS Target Properties-> Supported interface orientations(iPad). Then remove the layouts you do not need, then the trick will work.

Chao Qin
  • 41
  • 4
1

Open Info.plist, under "Supported interface orientations (iPad)" remove the items you don't need.

Asteroid
  • 1,049
  • 2
  • 8
  • 16