Ashutosh
Ashutosh

Reputation: 135

Correct method to check interface orientation

What is the correct method to check the interface orientation of device. Should I check status bar orientation or the orientation property of current active view controller?

Also can someone explain me the scenarios where the device orientations like face up and face down will be useful?

Upvotes: 0

Views: 1104

Answers (3)

Ashvin
Ashvin

Reputation: 9027

You can use bellow code..

May this will help you.

if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation))
    {     
       //  Orientation is   Portrait   
    }       
    if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation)) 
    {       
       // Orientation is Landscape        
    }

Upvotes: 1

JRG-Developer
JRG-Developer

Reputation: 12663

If the code is outside a method with the orientation as input, we use:

[[UIApplication sharedApplication] statusBarOrientation]

We have it in a few of our apps, and it hasn't seemed to cause any trouble... :D

Further, there isn't much difference between UIInterfaceOrientationPortrait and UIInterfaceOrientationPortraitUpsideDown, except where the home button is relative to the screen orientation (upside down is well... upside down...)

Some developers choose not to support the UIInterfaceOrientationPortraitUpsideDown mode just because it seems awkward to hold the device with the home button on top, but you can choose to implement and support it if you want to (all of our apps do).

Here's more info on the possible values for this and descriptions for each:

http://developer.apple.com/library/ios/#documentation/uikit/reference/UIApplication_Class/Reference/Reference.html#//apple_ref/c/tdef/UIInterfaceOrientation

Edit:

Please note, however, that iOS 6 changes some things with how UIViewControllers work, as shouldAutorotateToInterfaceOrientation has been deprecated. See this post:

How to force a UIViewController to Portrait orientation in iOS 6

Here's also the Apple docs on UIViewController which gives further details:

http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/DeprecationAppendix/AppendixADeprecatedAPI.html#//apple_ref/occ/instm/UIViewController/shouldAutorotateToInterfaceOrientation:

Upvotes: 1

Igor N
Igor N

Reputation: 381

If you are looking to find out which way the user sees the interface then use the interface orientation. I suppose both methods are fine.

If you want to find out which way the physical device is positioned use the device orientation.

Note that interface orientation is different from the device orientation (so there is no such thing as "interface orientation of device").

Device orientation can be flat up/flat down (Interface can only be portrait/upside down/landscape right/left). Device orientation might also be different if the device is rotated but the interface is set to a constant one. Plus landscape orientations are reversed e.g. device landsapeRight == interface landscapeLeft.

I am not sure when face up/down might be useful.

Just in case you need the actual methods, you can check the interface orientation from a view controller using this:

UIInterfaceOrientation orientation = self.interfaceOrientation;

Or from any class in the app using this (status bar check):

UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];

Get the device orientation like this:

UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];

Upvotes: 1

Related Questions