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

When you develop iOS app, you may want your app 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 show 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 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 left project navigator panel.
  3. Then click one target under TARGETS list in center editor panel .
  4. Click the General tab in target detail 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 select item in the Devices drop down list. Universal means it support both iPhone and iPad.
    select ios app supported device in xcode target deployment info

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

You can configure which device orientation dose your iOS app support in 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 checkbox.

  1. Portrait : If you check this checkbox only, it means the iOS app support Portrait orientation only. So that what ever direction your iOS device turns to, the app will only display in Portrait orientation. The app will not change it’s orientation to fit the device’s orientation change. You can see the effect in below video. You can press Command + Arrow Key to change the iOS simulator screen direction. 
  2. 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 bellow error message when you run the app. So this checkbox should be used with other orientation 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’
  3. Landscape Left : When you check this checkbox only, it will display the iOS app in landscape left orientation, whether you change your iOS device’s direction or not, the app’s orientation will not change.
  4. Landscape Right : This is similar with 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.

Besides configure 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 below function in Xcode project AppDelegate.swift file.

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask

Add below 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) )
}
READ :   iOS Swift Array Example

Leave a Reply

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.