Rotating child UIView when parent UITabBar or UINavigationController isn’t rotatable!


I’m building an app at the moment, with what is most likely a pretty fairly use case. It is a tab based app (Using StoryBoard/ iOS5.0/ XCode 4.3.2) , so at the top I have a UITabBarController, UINavigationController’s coming off of a few different tabs, then UIViewControllers coming off of those.

I DON’T want the home screen’s to be rotatable, but I do want one of my child UIView’s (a picture gallery, of course!) to be rotatable.

My UITabBarController at the root of the application had the following method;

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

This will make sure the views at the top don’t rotate, so I figured I would just add add the following to my gallery view controller to make just that one rotatable;

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{   
    return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}

But I found nothing happened when I rotated. I set a bunch of break points and found the method in my gallery detail controller wasn’t even hit, but the parent TabBarControllers method was still being hit. The trouble is, you can’t rotate a view unless it’s parent is marked as rotatable.

After some head scratching, the solution was actually pretty simple- I created a switch on my root TabBarController “allowRotation”, then adjust the shouldAutorotateToInterfaceOrientation method in the tabBarController to;

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if (self.allowRotation)
        return YES;
    else
        return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

Then in my gallery detail view I set that switch to true when you enter the view;

-(void) viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    RootTabBarController *tabcontrol = (RootTabBarController*)[self tabBarController];
    tabcontrol.allowRotation = YES;
}

And when you click the back button at the top of the view, I quickly set the switch back to false;

- (void)willMoveToParentViewController:(UIViewController *)parent
{
    RootTabBarController *tabcontrol = (RootTabBarController*)[self tabBarController];
    tabcontrol.allowRotation = NO;
}

I tried doing this on viewWillDisappear and a bunch of other methods, but found none of them fired early enough to get in there before the back button fired off it’s magic to prepare the parent views rotation.

NOTE: Make sure you’re app has rotation enabled in the Project Target “Summary” sheet, otherwise none of this will work!

  1. #1 by Gareth on June 26, 2012 - 13:18

    That Storyboard looks interesting.

  2. #2 by Oleksandr on September 18, 2012 - 17:35

    Thank”s so much!!! Spended 2 days for finding solution!!!

(will not be published)


I absolutely love these. They came at the lace wigs are good quality, and very soft. Absolutely love these extensions. I just love this hair. Hair came on time, I waited until after hair extensions uk very full, no shedding and it is been installed for 3 weeks now and yes I jus ordered 3 more bundles. Lol. I definitely recommend this company for your bundles ladies. They are soft, hair bundles on the head for human hair wigs and I would continue to recommend them. I have thinner hair so they cover and blend nicely.