How To Set iOS App Supported Devices And Orientation In Xcode Target Or Programmatically

When you develop an iOS app, you may want your app to support iPhone, iPad only, or support both of them. And in some cases, if your UI layout is very complex, you can make your iOS app layout fixed by showing the app only in one orientation ( for example Portrait or Landscape orientation only ) so that it can reduce your coding effort.

All these settings can be achieved in the Xcode project target configuration, as you know each target will be built to one iOS app product. And you can also configure it programmatically in swift source code. This article will tell you how to do it.

1. Configure iOS App Supported Devices In Xcode Target Steps.

  1. First, create an iOS project in Xcode.
  2. Click the Xcode project name in the left project navigator panel.
  3. Then click one target under the TARGETS list in the Xcode center editor panel.
  4. Click the General tab in one target detail information pane, and scroll down to the Deployment Info section. Then you can change the iOS app supported iOS device in the drop-down list.
  5. You can select either iPhone, iPad, or Universal item in the Devices drop-down list. Universal means it supports both iPhone and iPad.
    select-ios-app-supported-device-in-xcode-target-deployment-info

2. Configure iOS App Supported Orientation In Xcode Target Steps.

  1. You can configure which device orientation does your iOS app support in the Xcode target —> General —> Deployment Info section also. Just check the checkbox in the Deployment Info —> Device Orientation section. Below is the explanation of each orientation meaning, you can check multiple checkboxes.
  2. Portrait: If you check this checkbox only, it means the iOS app support Portrait orientation only. So that whatever direction your iOS device turns to, the app will only display in Portrait orientation. The app will not change its orientation to fit the device’s orientation change. You can see the effect in the below video. You can pressCommand + Arrow Keyto change the iOS simulator screen orientation. You can see this step demo video on URL https://youtu.be/A7iLU44UJpE.
  3. Upside Down: This orientation means you change the orientation of your iOS device 180 degrees. But if you check this checkbox only, you will get the below error message when you run the app. So this checkbox should be used with other orientations such as Portrait or Landscape.
    2019-09-18 10:36:13.815948+0800 TestProject[4709:145912] *** Terminating app due to uncaught exception 'UIApplicationInvalidInterfaceOrientation', reason: 'Supported orientations has no common orientation with the application, and [TestProject.ViewController shouldAutorotate] is returning YES'
  4. Landscape Left: When you check this checkbox only, it will display the iOS app in the landscape left orientation, whether you change your iOS device’s orientation or not, the app’s orientation will not change. You can see this step demo video on URL https://youtu.be/zODuFpd01PU.
  5. Landscape Right: This is similar to Landscape Left, the only difference is that it will display the iOS app UI components in landscape orientation right side.
    set-ios-app-landscape-right-orientation-in-xcode

3. How To Set iOS App Supported Orientation Programmatically.

  1. Besides configuring the iOS app-supported orientation in Xcode target —> General —> Deployment Info —> Device Orientation section, you can also configure it in swift source code programmatically. What you need to do is to override the below function in the Xcode project AppDelegate.swift file.
    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask
  2. Then add the below swift source code in your Xcode project’s AppDelegate.swift file.
    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
        
        // Support only one orientation value.
        //return UIInterfaceOrientationMask.portrait
        
        // Support multiple orientation values.
        return ( UIInterfaceOrientationMask(rawValue: UIInterfaceOrientationMask.portrait.rawValue | UIInterfaceOrientationMask.landscape.rawValue | UIInterfaceOrientationMask.portraitUpsideDown.rawValue) )
    }

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.