How to prevent rotation of view controllers in a tab bar controller?

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 on one of the tabs to rotate. Everything works almost fine: the controllers on the other tabs do not rotate. However, when the view controller that is allowed to rotate actually rotates, if the user deletes one of the remaining tabs, the corresponding view controller also rotates (although its toAutorotateToInterfaceOrientation: method explicitly returns NO).

How to prevent this?

To be clear, here is an example. By clicking on the tabs 0.1 or 2 and trying to rotate the device, nothing happens (correctly). By clicking on tab 4 and rotating the device, the view associated with the view controller on tab 4 rotates (correctly). Now, holding the iPhone in the rotational orientation of the landscape and clicking on another tab (0.1 or 2), you will see a rotated image (which is wrong and what I'm trying to avoid).

+3
source share
1 answer

This is a commonly reported “error,” however a good solution is to force the selector to turn on shouldAutorotateToInterfaceOrientation:as follows:

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

Source: https://habr.com/ru/post/1720764/


All Articles