Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining the Orientation of the screen rather than the orientation of the device

Tags:

The [[UIDevice currentDevice] orientation] method returns a number of orientations beyond the portrait and landscape orientation. I am well aware of checking to see if the orientation returned is "valid", unfortunately if the orientation returned is NOT "valid" when my app requires a specific orientation to determine which methods to run, I have no way of knowing which method is appropriate.

I have attempted a number of solutions to this, but for now the only thing I have been able to work out is to check to see if the orientation is LandscapeLeft or LandscapeRight and if it isn't I assume the screen is in portrait orientation. Unfortunately this isn't true when the device is face up and the screen is in landscape orientation.

I attempted to use the parent view controller's orientation:

parentController.interfaceOrientation;

Unfortunately it returned UIDeviceOrientationUnknown. I search SO and Google for someone else who has faced this problem. It seems silly to me that Apple would have anything but LandscapeLeft/Right and PortraitUp/Down for their orientations.

What I really need is the APPS orientation not the devices, since these are sometimes at odds. Any advice?

like image 489
Joshua Avatar asked May 16 '11 19:05

Joshua


People also ask

What determines screen orientation?

The Android or iOS software then uses the accelerometer's data to tell how you're holding your phone and orients the screen appropriately so that when you want to switch from browsing the web to watching a wide-screen video, the screen rotates automatically.

What is screen orientation?

Screen Orientation, also known as screen rotation, is the attribute of activity element in android. When screen orientation change from one state to other, it is also known as configuration change.

What can I use instead of Windows orientation?

I think the more stable solution is to use screen instead of window, because it could be both - landscape or portrait if you will resize your browser window on desktop computer. This works in IE 10, Firefox 23 and Chrome 29 (all desktop browsers).


1 Answers

Have you tried using UIInterfaceOrientationPortrait/UIInterfaceOrientationLandscape? I usually have pretty good results using these methods:

if(UIInterfaceOrientationIsPortrait(viewController.interfaceOrientation)) {
    //do portrait work
} else if(UIInterfaceOrientationIsLandscape(viewController.interfaceOrientation)){
    //do landscape work
}

You might also try UIDeviceOrientationIsValidInterfaceOrientation.

These will only return the app's orientation, which may not be the same as the device's orientation.

If those don't work, you can also try:

[[UIApplication sharedApplication] statusBarOrientation]
like image 168
Chamara Paul Avatar answered Sep 19 '22 17:09

Chamara Paul