Uinavigation Controller的视图控制器的混合方向
-
08-10-2019 - |
题
我有一个应用程序 UITabBarController
作为基础控制器 UINavigationController
对于两个标签。 TAB的主视图控制器都支持肖像和景观取向,但孙子视图仅在景观中。
我遇到的问题是,如果您在进入孙子视图之前从肖像开始,孙子的视野仍在肖像中显示 ChildViewController's
shouldAutorotateToInterfaceOrientation
仅针对景观模式返回。也就是说,导航栏朝向设备的顶部,就好像它处于肖像模式一样,而新的视图控制器并未旋转到强制方向。看起来像这样:
| [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
返回是的,以支持肖像和景观取向 ChildViewController's
shouldAutorotateToInterfaceOrientation
归还是仅出于景观取向:
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation
{
return (orientation == UIInterfaceOrientationLandscapeLeft ||
orientation == UIInterfaceOrientationLandscapeRight);
}
解决方案
解决此问题的方法是通过调整视图变换,如此问题所建议:
其他提示
我写了一个扩展程序,可以准确地完成您需要的事情: https://github.com/piercifani/tabbarbetterterrotation
子类 UITabBarController
并实施 shouldAutorotateToInterfaceOrientation
为了它
不隶属于 StackOverflow