felix_xiao
felix_xiao

Reputation: 1628

Swift - Allow Rotation on iPad Only

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?

Upvotes: 53

Views: 15642

Answers (6)

Pavlo28
Pavlo28

Reputation: 1596

Xcode 15 has separate values for iPhone and iPad

enter image description here

Upvotes: 0

Dion
Dion

Reputation: 3335

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
}

Upvotes: 0

alizx
alizx

Reputation: 1188

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.

Upvotes: 9

Kilenaitor
Kilenaitor

Reputation: 664

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)
    }
}

Upvotes: 10

chrisamanse
chrisamanse

Reputation: 4319

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

Upvotes: 142

Caleb
Caleb

Reputation: 124997

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.

Upvotes: 2

Related Questions