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

这一生的挚爱 提交于 2020-01-02 06:58:04

问题


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


回答1:


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];
}


来源:https://stackoverflow.com/questions/1606066/how-to-prevent-the-view-controllers-in-a-tab-bar-controller-from-rotating

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!