Search code examples
iphoneobjective-ciosxcodeuiinterfaceorientation

UITabBar identifying portrait or landscape orientation


While, for the most part, orientation is working properly for my app, I'm having an issue testing on my iPad 1. If I have the device tipped at a relatively low angle, there are times while navigating through the tabs that the tab bar appears in landscape mode, but the page calls a portrait mode uiview and then tries to render it in landscape mode, screwing up my UI.

I'm trying to figure out if there is a method to lock down "if the tab bar appears in landscape mode, always call the landscape UIViews and if in portrait mode, always call the portrait UIView."

On each view controller I've set the following:

- (void)viewDidLoad
{
[super viewDidLoad];

// iPad-specific condition here
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
    if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation)){
        self.view = self.portraitViewiPad;
    }
    else {
        self.view = self.landscapeViewiPad;
    }
}
}

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {

// iPad-specific condition here
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
    if (toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {

        //show portrait XIB here
        self.view = self.portraitViewiPad;

    } else {

        //show landscape XIB here
        self.view = self.landscapeViewiPad;

    }
}
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    {
    // iPad-specific interface here
    return YES;
}
else
{
    // For iPhone and iPod touch interface
    return (interfaceOrientation == UIInterfaceOrientationPortrait);

}
}

I've also adjusted the app delegate using the method below thinking that could address the issue:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
//CALLS RELOAD METHODS HERE AND EACH UIVIEW IS PROPERLY BEING CALLED
}

UPDATE:

Corrected this issue by checking the orientation of the status bar and displaying the correct uiview accordingly. Here's how I updated my viewDidLoad methods:

if ([[UIApplication sharedApplication] statusBarOrientation] == UIDeviceOrientationLandscapeLeft){

    NSLog(@"Left landscape detected");

    self.view = self.landscapeViewiPad;


} else if ([[UIApplication sharedApplication] statusBarOrientation] == UIDeviceOrientationLandscapeRight){

    NSLog(@"Right landscape detected");

    self.view = self.landscapeViewiPad;

} else if ([[UIApplication sharedApplication] statusBarOrientation] == UIDeviceOrientationPortrait){

    NSLog(@"Portrait orientation detected");

    self.view = self.portraitViewiPad;

} else if ([[UIApplication sharedApplication] statusBarOrientation] == UIDeviceOrientationPortraitUpsideDown){

    NSLog(@"Upsidedown Portrait detected");

    self.view = self.portraitViewiPad;

}

Solution

  • I think that you don't want to do test [[UIDevice currentDevice] orientation]. As the documentation notes that value can be different than the actual orientation of your app's UI. I think you'll want to rely on the calls to [UIViewController shouldRotateTo...] and [UIViewController willRotateTo...].