我有一个应用程序,它具有两个选项卡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);
}