views:

128

answers:

1

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:)

+1  A: 
Andiih
Hi Andiih and Thanks:) that does simplify things a lot.How do I get the navigationBar to rotate as well?
RickiG
Ahh just tried out the obvious: self.navigationController.navigationBar.transform = CGAffineTransformIdentity; self.navigationController.navigationBar.transform = CGAffineTransformMakeRotation(90.0*0.0174532925); self.navigationController.navigationBar.bounds = CGRectMake(0.0f, 0.0f, 480.0f, 30.0f); self.navigationController.navigationBar.center = CGPointMake(160.0f, 240.0f); thanks again.
RickiG