ProgrammingGuide > Basic Concepts and Essentials > Device Orientation

Device Orientation

Set Device Orientation for IOS

There are four device orientations for IOS:

  • Portrait
  • Upside Down
  • Landscape Left
  • Landscape Right

You can also view them in your project's target's General tab, look at the following screenshot:

NB: Pay special attention to the red outline box.

ios_device_orientation

When you create a new cocos2d-x project, the default settings are Landscape Left and Landscape Right.

Change iOS orientation from Landscape to Portrait.

Now it's time to change our default Landscape orientation to Portrait.

  1. Open up the RootViewController.mm under the ios group:

rootviewcontroller22

and find the following code:

1
2
3
4
5
6
7
8
9
10
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return UIInterfaceOrientationIsLandscape( interfaceOrientation );
}

// For ios6, use supportedInterfaceOrientations & shouldAutorotate instead
- (NSUInteger) supportedInterfaceOrientations{
#ifdef __IPHONE_6_0
    return UIInterfaceOrientationMaskAllButUpsideDown;
#endif
}

You should replace it with the following if you want to force the device orientation to Portrait mode:

1
2
3
4
5
6
7
8
9
10
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return UIInterfaceOrientationIsPortrait( interfaceOrientation );
}

// For ios6, use supportedInterfaceOrientations & shouldAutorotate instead
- (NSUInteger) supportedInterfaceOrientations{
#ifdef __IPHONE_6_0
    return UIInterfaceOrientationMaskPortrait;
#endif
}

You could hold command and click the macro UIInterfaceOrientationMaskPortrait to see other options.

Now, build and run it from Xcode, it should be the Portrait mode now.

NB: If you want to change your game to portrait, you should remove the Landscape option, otherwise in iOS8+, the display would be wrong.

Change Device Orientation for Android

Find and open the AndroidManifest.xml file under your proj.android directory:

manifest

and find the following code:

androidorientation

If you want to change your app to portrait, you need to change the landscape to portrait.

ios_device_orientation.png (211.5 kB) owen, 2014-04-09 08:27

manifest.png (97.9 kB) owen, 2014-04-09 08:40

androidorientation.png (121.6 kB) owen, 2014-04-09 08:40

rootviewcontroller.png (55.8 kB) owen, 2014-04-09 08:40

Sign up for our newsletter to keep up with the latest developments, releases and updates for Cocos2d-x.