Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check in which position (landscape or portrait) is the iPhone now?

I have an app with a tab bar, and nav controllers in each tab. When user shakes the device, a UIImageView appears as a child view in the nav controller. But the UIImageView must contain a special image, depending on the device's current orientation.

If I write just

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)    if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {     //Code    }    else if (interfaceOrientation == UIInterfaceOrientationLandscapeRight||interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {    //Code    } } 

The view just goes crazy if user rotated the device before shaking.

Is there a method to get iPhones current orientation?

like image 632
Knodel Avatar asked Mar 25 '10 14:03

Knodel


People also ask

Why is my phone screen not rotating?

To adjust the screen rotation settings: Swipe down from the top of the screen to open the Quick settings panel. Look for the screen orientation icon. Depending on your settings, you may need to look for the Portrait, Landscape, or Auto Rotate icon.


2 Answers

Here are macros UIDeviceOrientationIsLandscape and UIDeviceOrientationIsPortrait

so rather checking separately you can do it like this ...

if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation)) {     // code for landscape orientation       } 

OR

 if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation))  {      // code for Portrait orientation         } 
like image 54
Azhar Avatar answered Oct 04 '22 11:10

Azhar


Use the [[UIDevice currentDevice] orientation] method, as specified here.

like image 35
Tim Avatar answered Oct 04 '22 12:10

Tim