R – How to prevent the view controllers in a tab bar controller from rotating

iphonerotationtabbarcontroller

I have a tab bar controller managing 4 tabs. I have subclassed the tab bar controller so that the shouldAutorotateToInterfaceOrientation: method only allows a specific view controller in one of the tabs to rotate. Everything works almost fine: the controllers in the remaining tabs do not rotate. However, when the view controller which is allowed to rotate actually rotates, if the user taps one of the remaining tabs, the corresponding view controller also appear rotated (even though its shouldAutorotateToInterfaceOrientation: method explicitly returns NO).

How do I prevent this from happening?

To be clear, here is an example. Tapping on the tabs 0,1, or 2 and trying to rotate the device, nothing happens (correctly). Tapping on the tab 4 and rotating the device, the view associated to the view controller of tab 4 is rotated (correctly). Now, still holding the iPhone in the rotated landscape orientation and tapping another tab (0,1, or 2) reveal a rotated view (which is not correct and what I am trying to avoid).

Best Answer

This is a commonly reported "bug" - however a good workaround is to force the shouldAutorotateToInterfaceOrientation: selector to be triggered as follows:

- (void)viewDidAppear:(BOOL)animated {
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    UIView *view = [window.subviews objectAtIndex:0];
    [view removeFromSuperview];
    [window addSubview:view];
}
Related Topic