简体   繁体   中英

lock iPad to landscape orientation

I have a game that I only want to be played in landscape mode. This is fine for iPhone but not fine for iPad. I thought I could get around this by releasing 2 versions of the same app, one for iPhone and one for iPad. However, after looking further I've found that the iPhone app needs to be iPad ready? I've seen this answer that suggests that it is possible to lock the orientation of an iPad app:

Lock Ipad App in Landscape mode

Is this still possible in the later versions of xCode and swift, and if so, how??

Here's what you can do. in the viewDidLoad method in every ViewController class insert this code.

if UIDevice.currentDevice().userInterfaceIdiom == .Pad {

            // this code will be executed if the device is an iPad
          override  func supportedInterfaceOrientations() -> Int {
              return UIInterfaceOrientation.LandscapeLeft.rawValue
         }

          override func shouldAutorotate() -> Bool {
                  return false
          }

    }

What will happen is the following

  1. The viewDidLoad function is executed and the if statement is evaluated
  2. The condition for the if statement is true so the statements inside the block should be executed
  3. The first override function will set the orientation to landscapeleft. You can change that to landscaperight
  4. The second function will lock the orientation rotation functionality

NOTE: I prefer that you embed all your view controllers inside a navigation controller and linking this navigation controller to a class then writing the code i gave you in the viewDidLoad method of this class. This will make the configuration for all the views inside this navigation controller rather than typing them in every class

If one has to lock the orientation for all the screens and for iPhone and iPad, you can easily achieve the same directly in Xcode.

Go into Targets>General>Deployment Info and the select the required device orientation. Please check the screenshot. 在此处输入图片说明

In this way you wont have any problem with the Launch Image

为什么不从info.plist的“支持的界面方向(iPad)”下删除您不需要的方向

我相信这可以在xcode文件导航器顶部的蓝色文件的部署信息下完成

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM