12

I have the following code in a method. When I run this in the simulator the debugger skips right over the code?? What am I missing?

if (([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft) || 
        ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight)) 
{       

} else {

}
Matteo Alessani
  • 10,264
  • 4
  • 40
  • 57
Jordan
  • 21,746
  • 10
  • 51
  • 63

8 Answers8

54

The best way to determine interface orientation is to look at status bar orientation:

 UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];

    if(orientation == UIInterfaceOrientationPortrait || 
       orientation == UIInterfaceOrientationPortraitUpsideDown) {

       //Portrait orientation

}

if(orientation == UIInterfaceOrientationLandscapeRight ||
   orientation == UIInterfaceOrientationLandscapeLeft) {

    //Landscape orientation

}

UIDevice class measures orientation based on accelerometer and if device lays flat, it won't return the correct orientation.

brainray
  • 12,512
  • 11
  • 67
  • 116
srgtuszy
  • 1,548
  • 1
  • 18
  • 16
  • `UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];` combined with the `UIDeviceOrientationIsLandscape` and `UIDeviceOrientationIsPortait` is the way to go. – Joe Mar 31 '13 at 02:15
  • Wonderful, thanks! Why is Apple recommending code that can easily be problematic? https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/RespondingtoDeviceOrientationChanges/RespondingtoDeviceOrientationChanges.html#//apple_ref/doc/uid/TP40007457-CH7-SW1 – brainray Jan 28 '14 at 23:02
23

Note that there's a macro UIDeviceOrientationIsLandscape and UIDeviceOrientationIsPortrait, so instead of comparing it separately to LandscapeLeft and LandscapeRight you could just do it like this:

if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
{
}
inkredibl
  • 1,918
  • 1
  • 14
  • 19
18

Update 2

This shouldn't matter, but try turning on orientation notifications:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];


[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(detectOrientation) name:@"UIDeviceOrientationDidChangeNotification" object:nil];

Update

My bad, I assumed it was empty.

Try removing the or statement and just test for a single orientation. See if that fixes it. Maybe there is a bracket problem or something silly.

I have the following test working in production code, so your technique should work:

    if (([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft) || 
        ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight)) {


}

Original Answer

You have to actually put statements in the if blocks to get it to step in.

The debugger is smart enough to skip over empty blocks.

Corey Floyd
  • 25,929
  • 31
  • 126
  • 154
2

Another way of doing this without turning on orientation notification would be to

Step 1: Save the current orientation in a local variable myCurrentOrientation and assign it like this:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
                                duration:(NSTimeInterval)duration
{
    myCurrentOrientation = toInterfaceOrientation;
}

Step 2: Use myCurrentOrientation for your check

if (UIInterfaceOrientationIsLandscape(myCurrentOrientation) == YES) {
    // landscape
}
else {
    // portrait.
}
S B
  • 8,134
  • 10
  • 54
  • 108
  • Good advice. But beware that this code isn't called when user changes orientation in another view and return here. Workaround by adding this in a convenient method: myCurrentOrientation = [[UIApplication sharedApplication] statusBarOrientation]; – FeltMarker Aug 27 '12 at 13:06
0

I recommend you to use my highlighted code instead of yours to safe some code of lines.

-(void) viewDidLoad
{
    [super viewDidLoad];
    [self rotations];
}

-(void)rotations
{
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(orientationChanged:)
                                         name:UIDeviceOrientationDidChangeNotification
                                         object:nil];
}

-(void) orientationChanged:(NSNotification *)notification
{
    //USE THIS PART
    //USE THIS PART
    //USE THIS PART
    //USE THIS PART
    //USE THIS PART
    if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation))
    {
    }
}

INSTEAD OF

if([[UIDevice currentDevice] orientation] == UIInterfaceOrientationPortrait || 
   [[UIDevice currentDevice] orientation] == UIInterfaceOrientationPortraitUpsideDown) 
{
}
Jiří Zahálka
  • 8,070
  • 2
  • 21
  • 17
0

Here is a method to find the orientation and the true center of the screen. I used Tuszy's method so I could set UIActivityIndicatorView properly.

- (BOOL) isPortraitOrientation {
    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
    if(orientation == UIInterfaceOrientationPortrait ||
       orientation == UIInterfaceOrientationPortraitUpsideDown) {
        return true;
    }
    if(orientation == UIInterfaceOrientationLandscapeRight ||
       orientation == UIInterfaceOrientationLandscapeLeft) {
        return false;
    }
    return false;
}

And the way to get center...

- (void) findMyUIViewCenter {
    CGPoint myCenter;
    if ([self isPortraitOrientation]) {
        myCenter = self.view.center;
    }
    else {
        myCenter = CGPointMake(self.view.frame.size.height / 2.0, self.view.frame.size.width / 2.0);
    }
    NSLog(@"true center -- x:%f y:%f )",myCenter.x,myCenter.y);
}
MGM
  • 2,379
  • 1
  • 19
  • 13
0

Heh you need to call [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications] before obtaining the value. Have a look at documentation of this method. Took me a while to track this down.

EdChum
  • 376,765
  • 198
  • 813
  • 562
Filip
  • 1
0

Say you are inside a Springboard tweak and want to show something depending on the orientation of the current app, then you can use this (jailbreak only):

UIInterfaceOrientation o = [[UIApplication sharedApplication] _frontMostAppOrientation];
malhal
  • 26,330
  • 7
  • 115
  • 133