Tabbar controller with navigationcontrollers orien

2019-03-22 05:00发布

问题:

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..

回答1:

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