Different Device Orientation according to device (iPhone or iPad)

若如初见. 提交于 2019-12-30 02:13:26

问题


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


回答1:


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.

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>



回答2:


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
    }


来源:https://stackoverflow.com/questions/29664441/different-device-orientation-according-to-device-iphone-or-ipad

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!