开发者

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

开发者 https://www.devze.com 2022-12-09 17:28 出处:网络
I have a tab bar controller managing 4 tabs. I have subclassed the tab bar controller so that the shouldAutoro开发者_如何学CtateToInterfaceOrientation: method only allows a specific view controller in

I have a tab bar controller managing 4 tabs. I have subclassed the tab bar controller so that the shouldAutoro开发者_如何学CtateToInterfaceOrientation: 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).


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号