Swift - Allow Rotation on iPad Only

IosSwiftIpad

Ios Problem Overview


How could I allow my universal app written in Swift on iOS 8.3 SDK to support only portrait mode on iPhone, but both portrait and landscape mode on iPad?

I know in the past this has been done in AppDelegate. How could I do this in Swift?

Ios Solutions


Solution 1 - Ios

You can do it programmatically, or better yet, you can simply edit your project's Info.plist (Which should be more practical, since it's a global device configuration)

Just add "Supported Interface orientations (iPad)" key

enter image description here

Solution 2 - Ios

You could do it programmatically

override func shouldAutoRotate() -> Bool {
    if UIDevice.currentDevice().userInterfaceIdiom == .Pad {
        return true
    }
    else {
        return false
    }
}

and then

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

or any other rotation orientation that you wish to have by default.

That should detect if the device you're using is an iPad and allow rotation on that device only.

EDIT: Since you only want portrait on iPhone,

 override func supportedInterfaceOrientations() -> Int {
    if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
        return UIInterfaceOrientation.Portrait.rawValue
    }
    else {
        return Int(UIInterfaceOrientationMask.All.rawValue)
    }
}

Solution 3 - Ios

I'm not sure if Chris's answer works by just copying and pasting "Supported Interface orientations (iPad)" key; probably not judging from the XML source of info.plist. for achieving different orientation support. you can open the info.plist XML source and edit it as follows:

<key>UISupportedInterfaceOrientations</key>
<array>
	<string>UIInterfaceOrientationPortrait</string>
</array>


<key>UISupportedInterfaceOrientations~ipad</key>
<array>
	<string>UIInterfaceOrientationPortrait</string>
	<string>UIInterfaceOrientationPortraitUpsideDown</string>
	<string>UIInterfaceOrientationLandscapeLeft</string>
	<string>UIInterfaceOrientationLandscapeRight</string>
</array> 

And there is an easeir approche from xcode UI. Go to your project settings. select your target in the "General" tab and then in "Deployment Info" section you can first select iphone/ipad and mark the device orientations you want to support for each device sepratly and then change the devices to "Universal". it will generate the above xml under the hood. enter image description here

the "Universal" option here shows the selections that are common between iPhone and iPad.

Solution 4 - Ios

> I know in the past this has been done in AppDelegate. How could I do this in Swift?

The language you use doesn't change the architecture of the application. You do this in Swift the same way you do it in Objective-C, i.e. by implementing:

optional func application(_ application: UIApplication,
         supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int

in your application delegate.

Solution 5 - Ios

If you want to set this for a specific ViewController (allow all on iPad but only portrait on iPhone) in Swift 4:

override var supportedInterfaceOrientations:UIInterfaceOrientationMask {
    return UIDevice.current.userInterfaceIdiom == .pad ? UIInterfaceOrientationMask.all : UIInterfaceOrientationMask.portrait
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionfelix_xiaoView Question on Stackoverflow
Solution 1 - IoschrisamanseView Answer on Stackoverflow
Solution 2 - IosKilenaitorView Answer on Stackoverflow
Solution 3 - IosalizxView Answer on Stackoverflow
Solution 4 - IosCalebView Answer on Stackoverflow
Solution 5 - IosDionView Answer on Stackoverflow