Different device orientations according to the device (iPhone or iPad)

I am working on a universal project with these requirements:

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

How to do it for iOS 8 (Swift)?

+6
source share
2 answers

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

Here is the print screen from the settings I use. Perhaps this may help other developers with the same doubt.

Settings in info.plist file

The relative source code is as follows:

<key>UISupportedInterfaceOrientations</key> <array> <string>UIInterfaceOrientationPortrait</string> </array> <key>UISupportedInterfaceOrientations~ipad</key> <array> <string>UIInterfaceOrientationLandscapeLeft</string> <string>UIInterfaceOrientationLandscapeRight</string> </array> 
+23
source

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

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

Then, as soon as you discover your hardware, block the orientation using the shouldAutorotateToInterfaceOrientation function:

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

As an example, you can 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 } 
0
source

Source: https://habr.com/ru/post/985232/


All Articles