0

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

3 回答 3

1

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

是否有记录的方法来设置 iPhone 方向?

于 2011-01-16T19:26:49.710 回答
0

子类化UITabBarControllershouldAutorotateToInterfaceOrientation为其实现

于 2011-01-07T20:52:44.283 回答
0
于 2012-08-18T19:54:14.670 回答