Autorotate ignored when changing tabs

爱⌒轻易说出口 提交于 2019-12-05 07:54:09

问题


I have an application with a UITabBarController, each of the tabs has a UINavigationController "attached" to it. Now lets assume that the rootViewControllers (of the navigationControllers) in Tabs 1,2 and 4 only support the portrait orientation and have such a "shouldAutorotateToInterfaceOrientation" implementation that only returns YES when asked to rotate to portrait. Tab 3 however has some viewControllers in its navigationController that support landscape orientation.

When I am in tab 3 now and move to one of the viewControllers that support landscape I am able to turn the device and the interface changes to landscape. Yet if I hit tab 1,2 or 4 with the interface in landscape mode, the interface does not get changed back to portrait but stays in landscape, despite the fact that the displayed viewControllers clearly only support portrait.

I am unsure as to what I am missing or whether this is intended behavior, I would like the interface orientation to switch back to portrait once I switch to a portrait only viewController via the tabBarController. The entire hierarchy is constructed programmatically.

Thanks!


回答1:


I was having the same issue as you on one of my apps, with the difference that I was not using navigation controllers in the tab items.

I ended up creating a category for the UITabBarController (since it's not meant to be subclassed) for the method shouldAutorotate...

@implementation UITabBarController (orientation)

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
#if DEBUG 
    NSLog(@"UITabBarController (orientation) -> shouldAutorotateToInterfaceOrientation: [%d]",toInterfaceOrientation);
#endif  
    //if(toInterfaceOrientation == UIInterfaceOrientationPortrait) return YES;
//  else return [self.selectedViewController shouldAutorotateToInterfaceOrientation:toInterfaceOrientation];
    if (self.selectedViewController == [self.viewControllers objectAtIndex:kLibraryStoreTabIndex])
        return NO;


    if( self.selectedViewController == [self.viewControllers objectAtIndex:kContactTabIndex]){

        return YES;
    }
// rest of the conditions depending of the tab 

return NO; //last option 
}


来源:https://stackoverflow.com/questions/5301317/autorotate-ignored-when-changing-tabs

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