12

I'm working on a Universal project with these requirements:

  • For iPhone, I want only the portrait orientation.
  • For iPad, only landscapes.

How do I do that for iOS 8 (Swift)?

nhgrif
  • 61,578
  • 25
  • 134
  • 173
insetoman
  • 869
  • 6
  • 13
  • 2
    have you tried the info.plist in Xcode? – ScarletMerlin Apr 16 '15 at 02:47
  • Hi @ScarletMerlin . Thank you for your advice. I don't know how I missed the key "Supported interface orientations (iPad)". Using this key, it works according to the requirements. Feel free to create an answer so I can upvote. Cheers, mate. – insetoman Apr 16 '15 at 03:16
  • 1
    don't worry I don't need the points from the approved answer. The info.plist is how you tell the iphone to handle global settings. you could also do it on code, but it is hardly ever recommended. – ScarletMerlin Apr 16 '15 at 19:46

2 Answers2

45

Following the advice of @ScarletMerlin , changing the keys in info.plist is, in my opinion, the best solution for the requirements I have to fulfill (fixed orientation type for each kind of device).

Here is a print screen from the settings I use. Maybe it can help other developers with similar doubt.

Settings in info.plist file

The relavent source code looks like this:

<key>UISupportedInterfaceOrientations</key>
<array>
    <string>UIInterfaceOrientationPortrait</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
    <string>UIInterfaceOrientationLandscapeLeft</string>
    <string>UIInterfaceOrientationLandscapeRight</string>
</array>
nhgrif
  • 61,578
  • 25
  • 134
  • 173
insetoman
  • 869
  • 6
  • 13
  • 1
    Does this override whatever is checked under Target > General > Deployment Info > Device Orientation ? –  Nov 30 '18 at 07:33
0

My suggestion is to check the hardware your application is running on. To do so, use this line of code.

if([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad)

Then, once you have detected your hardware, block the orientation by using the shouldAutorotateToInterfaceOrientation:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation
{
    return UIInterfaceOrientationIsLandscape(orientation);
}

Just as an example, you could do this

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation
    {
        if([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) 
            return UIInterfaceOrientationIsLandscape(orientation); // If iPad
        else
            return UIInterfaceOrientationIsPortrait(orientation); // Else, it is an iPhone
    }