在TabBar驱动的应用程序中UINavigationController的视图控制器的混合方向

时间:2011-01-07 19:54:12

标签: iphone uiviewcontroller uitabbarcontroller screen-orientation

我有一个应用程序,UITabBarController作为基本控制器,UINavigationController用于两个选项卡。两个选项卡的主视图控制器都支持纵向和横向,但孙视图只需要在横向上。

我遇到的问题是,如果你在进入孙子视图之前以纵向开始,即使ChildViewController's shouldAutorotateToInterfaceOrientation仅为横向模式返回YES,也会以纵向显示孙子的视图。也就是说,导航栏朝向设备顶部显示,就像它处于纵向模式并且新视图控制器未旋转到强制方向一样。它看起来像这样:

| [Navigation bar] |
|                  |
|  View contents   |
|                  |
|                  |
|                  |

而不是正确的定向布局:

|  [Navigation bar]               |
|                                 |
|   View contents, rotated        |

如何确保孙子视图以正确的方向显示?以下是我如何设置所有内容:

// first creating one of two view controllers used for the tabbar

UINavigationController *firstNavController = [[UINavigationController alloc] init];    

ContentOneListController *listController = [[[ContentOneListController alloc] initWithNibName:@"ContentOneView"                                                                                    bundle:nil] autorelease];    
listController.tabBarItem.title = @"One";

firstNavController.viewControllers = [NSArray arrayWithObject:listController];

// view controllers are added to the tabbar controller here (ivar of the class)
tabBarController = [[UITabBarController alloc] init];   
tabBarController.viewControllers = [NSArray arrayWithObjects:
        firstNavController, secondNavController, nil];

[window addSubview:tabBarController.view];
[window makeKeyAndVisible];

// later on, I add a child view controller onto ContentOneListController

NSArray *viewControllers = tabBarController.viewControllers;
NSInteger selectedIndex = [tabBarController selectedIndex]; 
UINavigationController *selectedNavController = [viewControllers objectAtIndex:selectedIndex];  

// ChildViewController only supports landscape orientation
ChildViewController *childController = [[[ChildViewController alloc] initWithNibName:@"ChildView"                                                                                      bundle:nil] autorelease];

[selectedNavController pushViewController:controller animated:NO];

ContentOneListController's shouldAutorotateToInterfaceOrientation返回YES以支持纵向和横向,而ChildViewController's shouldAutorotateToInterfaceOrientation仅返回YES以用于横向:

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation 
{
    return (orientation == UIInterfaceOrientationLandscapeLeft ||
            orientation == UIInterfaceOrientationLandscapeRight);
}

3 个答案:

答案 0 :(得分:1)

解决此问题的方法是按照此问题中的建议调整视图转换:

Is there a documented way to set the iPhone orientation?

答案 1 :(得分:0)

子类UITabBarController并为其实施shouldAutorotateToInterfaceOrientation

答案 2 :(得分:0)

我编写了一个扩展程序,可以完全按照您的需要进行操作:https://github.com/piercifani/TabBarBetterRotation