UITabBarController with viewControllers utilizing different orientations?

Posted by RickiG on Stack Overflow See other posts from Stack Overflow or by RickiG
Published on 2010-06-09T19:29:02Z Indexed on 2010/06/09 19:32 UTC
Read the original article Hit count: 268

Hi

I can see that this is something that has been troubling a lot of people:/

I have a UITabBarController that has 4 viewControllers, all of type UINavigationController. One of the navigationControllers gets a viewController pushed onto its stack, this viewController should be presented in landscape mode/orientation.

The viewController is a graph, it is the absolutely only place in the app where landscape makes sense. (I hide the UITabBar when this is presented to not lead the user to believe this will work everywhere)

To make a UITabBarController respond correctly to changes in orientation all its viewControllers need to return the same value from the delegate method:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

So to accomodate this behavior I have implemented this method in all the viewControllers belonging to the UITabBarController:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    BOOL canRotate = [defaults boolForKey:@"can_rotate"];

    return canRotate;
}

The "trick" is now that when my can-be-landscape viewController is pushed I do this:

- (void) viewWillAppear:(BOOL)animated {

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setBool:YES forKey:@"can_rotate"];
    [defaults synchronize];

}

and when it is popped, I do this:

- (void) viewWillDisappear:(BOOL)animated {

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setBool:NO forKey:@"can_rotate"];
    [defaults synchronize];

}

This works really well. When the viewController is on the stack I can rotate the device and the view follows.

The problem is however, that if the user taps the "back" button on the navigationBar while in landscape mode, thus popping the viewController to the previous viewController, this "old" viewController is of course also in landscape mode. To make things worse, because I set the BOOL to NO, this "old" viewController can not rotate back when I orientate the device to portrait mode.

Is there a way to update everything so that none of my other viewControllers will be in landscape mode when I pop the can-be-in-landscape mode viewController?

I am a bit worried that if this could be done from landscape to portrait it should also be possible from portrait to landscape, thus making my "hack" unnecessary.. but if it can not, then I am back to square one :/

Hope I am close and that someone could help me get there, thanks:)

© Stack Overflow or respective owner

Related posts about iphone

Related posts about iphone-sdk