2

I'am currently working on an project where we have a tab bar controller with 4 tabs, and where each tab have an navigation controller. On each of these navigation controller there is multiple viewcontrollers pushed on it.

I read a lot of post here and other places, and we have currently done the following:

Subclassed UITabbarcontroller

- (BOOL)shouldAutorotate
{

    return [[[self.viewControllers objectAtIndex:self.selectedIndex]topViewController] shouldAutorotate];

}

- (NSUInteger) supportedInterfaceOrientations
{
    return [[[self.viewControllers objectAtIndex:self.selectedIndex]topViewController]supportedInterfaceOrientations];
}

- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
    return [[[self.viewControllers objectAtIndex:self.selectedIndex]topViewController] shouldAutorotateToInterfaceOrientation:toInterfaceOrientation];
}

This work fine, if we in each of our viewcontrollers specify the following:

- (NSUInteger) supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskPortrait;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
    return YES;
}

This will lock it to Portrait as expected.

But now the real problem occurs! If we in our viewcontroller on one of the tabs specify that it should rotate to landscape it works fine, but when we then change tab it is still in landscape, which is not what we want!

So to sum up, have anyone got a solution to how you lock almost all views to a given orientation expect one, and can change tabs where they are in the orientation you specified (here portrait)?

I also read this post iOS 6 UITabBarController supported orientation with current UINavigation controller, but as one comment also mentioned "This is almost working for me. The problem is if I am already in landscape when I switch tabs to a portrait only view it is still in landscape. Rotating portrait fixes it and it won't rotate back to landscape, but I still need it be in portrait when it first loads" which almost is the same here..

Community
  • 1
  • 1
Lasse
  • 362
  • 1
  • 10

1 Answers1

2

I myself had this problem and i worked out a solution, its not pretty but it works.

In your TabbarController subclass implement this tabbarcontroller delegate function (remember to set delegate):

-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    int selected = self.selectedIndex;
    UIViewController *con = [[UIViewController alloc] initWithNibName:@"XIBName" bundle:nil];
    [[self.viewControllers objectAtIndex:selected] pushViewController:con animated:NO];
    [[self.viewControllers objectAtIndex:selected]popViewControllerAnimated:NO];
    [[self.viewControllers objectAtIndex:selected] setDelegate:nil];
}

The push and pop on the uinavigationcontroller in the tabs navigationcontroller will make the tabbarcontroller fire its Orientations functions again, and if you implemented the orientations code correctly it will change to your desired orientation.

i hope this helps, please fell free to comment if i need to explain anything in details.

Best Regards Morten

MortenHN
  • 108
  • 10
  • Well it maybe aint pretty but it works as a charm! I can now switch the one view to landscape while others stays in portrait even if you switch tabs! Thank you many times for help! And as you say, important to set the delegate, i did it in viewwillappear in my subclass and works as expected! Hope this would help others too. – Lasse Nov 30 '12 at 14:14
  • Hmmm, isn't this messing with the internal memory management that iOS does with view controllers added to the tab bar controller? The issue we have is that our entire app has been Storyboarded, which means we can't be initialising view controllers in code. Is there a way round the problem for projects that use the Storyboard? – jowie Apr 18 '13 at 14:18
  • @jowie: you can add an empty view-controller to your storyboard, assign a storyboard-id and then call [self.storyboard instantiateViewControllerWithIdentifier:@"EmptyVC"]. – patric.schenke Nov 26 '13 at 12:45