views:

74

answers:

4

I use a tabBar Controller as root controller. It has 4 tabs and each of its ViewControllers has

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

as long as the tabBarController itself. But when I rotate the device (real or simulator), the screen turns at random! If it doesn't turn when I open the application it would have the same behavior until I quit the app. I tried to add the 4 viewControllers one by one in IB to see if one was problematic, but I obtained the same issue. It only always turns when there is no tabs at all!

Please tell me if you have any ideas. Thanks!

A: 

You have to subclass UITabBarController and implement shouldAutorotateToInterfaceOrientation:

Can Berk Güder
My current tabBarController is currently a subclass of UITabBarController and I have already implemented this function.
Baldoph
+1  A: 

You set every view controller to say that it responds to any possible orientation. Therefore, every view will attempt to rotate to every orientation.

Views don't really automatically rotate. You usually have to manage the placement of subview programmatically in all but the simplest views.

If you have no custom orientation code, you're probably seeing the views try to draw the portrait view in the landscape frame or vice versa. If you have autoresize subviews set your subviews will appear to scatter across the screen in a seemingly random pattern. The more you change orientation, the more random the placement becomes.

For complex views, I like to create separate viewController/view pairs for each orientation. Then I put the views in a nav controller. As the orientation changes, each view controller will push or pop the appropriate view controller for the coming orientation onto/off the stack. To the user, this looks like a single view is gracefully redrawing itself. (This is especially useful if you have non-standard UI elements that have to be manually rotated with transforms)

TechZen
A: 

Actually, I just want my first tab view controller to rotate. So I put this code in my custom tabBarController :

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
        if (self.selectedIndex == 0) {
            return toInterfaceOrientation != UIInterfaceOrientationPortraitUpsideDown;
        }else {
            return toInterfaceOrientation == UIInterfaceOrientationPortrait;
        }
}

but I had the same problem. I use a custom orientation code for my first tab view controller when turning to landscape. Called with the following function in my custom tabBarcontroller:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    if (toInterfaceOrientation == UIInterfaceOrientationPortrait) {
        //rotation to Portrait
        lastOrientation = toInterfaceOrientation;
        [[UIApplication sharedApplication] setStatusBarHidden:NO animated:NO];
        [self.selectedViewController willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
    }
    else if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {
        if (!UIInterfaceOrientationIsLandscape(lastOrientation)) {
            //rotation to Landscape
            [self.selectedViewController willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
        }
        lastOrientation = toInterfaceOrientation;
    }
}
Baldoph
A: 

I found that if you set the selected tab programmatically the tabViewController rotates erratically.

Baldoph