1
votes

I have an app that changes the view when the orientation changes using: [[UIApplication sharedApplication] statusBarOrientation]. On the iPhone, it works but on the iPad, it gives me landscape instead of portrait and portrait instead of landscape.

  1. When I call the getRelevantFrame method outside of the UIDeviceOrientationDidChangeNotification, it will return the correct frame.
  2. It will return the opposite frame (if landscape, will return portrait and vice versa) when responding to the notification.
  3. Both the versions (iPod/iPhone + iPad) uses the same code but this only breaks on the iPad version

Here is the code I used to calculate the relevant frame:
EDIT: Used rob5408's advice. Changed to use UI_USER_INTERFACE_IDIOM().

+ (CGRect)getRelevantFrame {
    //Get orientation
    UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];

   if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) { //iPhone/iPod
        if(interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
            return [self frameiPhoneLandscape];
        }
        else {
            return [self frameiPhonePortrait];
        }
    } else if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { //iPad
        if(interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
            NSLog(@"iPad Landscape");
            return [self frameiPadLandscape];
        }
        else {
            NSLog(@"iPad Portrait");

            return [self frameiPadPortrait];
        }
    }

    return CGRectMake(0, 0, 0, 0);
}

Here is the notification I used:

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

Here is the code I used to read the orientation change:

- (void) didRotate:(NSNotification *)notification
{
    NSLog(@"Parent: %@", self.parentController);
    if(self.parentController) {
        [self setFrame:[TASFrames getRelevantFrame]];
    }
}
1
Do yourself a solid and use UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad instead of using view bounds. That probably won't fix it, but it is going to save you problems in the future. - rob5408
Thank you for the advice. I will definitely use that from now on. - FireDragonMule

1 Answers

0
votes

I transferred the code to a new project. And now it works as it's supposed to. No idea as to why this happens. Code didn't change.