Different Device Orientation according to device (iPhone or iPad)

后端 未结 2 2050
[愿得一人]
[愿得一人] 2020-12-30 06:28

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

  • For iPhone, I want only the portrait orientation.
  • For iPad, only landscapes.
2条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-30 06:56

    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
        }
    

提交回复
热议问题