2

我目前正在开发一个项目,我们有一个带有 4 个选项卡的选项卡栏控制器,每个选项卡都有一个导航控制器。在这些导航控制器中的每一个上,都推送了多个视图控制器。

我在这里和其他地方阅读了很多帖子,我们目前做了以下工作:

子类化的 UITabbar 控制器

- (BOOL)shouldAutorotate
{

    return [[[self.viewControllers objectAtIndex:self.selectedIndex]topViewController] shouldAutorotate];

}

- (NSUInteger) supportedInterfaceOrientations
{
    return [[[self.viewControllers objectAtIndex:self.selectedIndex]topViewController]supportedInterfaceOrientations];
}

- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
    return [[[self.viewControllers objectAtIndex:self.selectedIndex]topViewController] shouldAutorotateToInterfaceOrientation:toInterfaceOrientation];
}

如果我们在每个视图控制器中指定以下内容,这可以正常工作:

- (NSUInteger) supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskPortrait;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
    return YES;
}

这将按预期将其锁定为纵向。

但现在真正的问题出现了!如果我们在其中一个选项卡上的视图控制器中指定它应该旋转到横向,它可以正常工作,但是当我们更改选项卡时它仍然是横向的,这不是我们想要的!

总而言之,有没有人找到解决方案来解决如何将几乎所有视图锁定到给定方向(期望一个),并且可以更改它们在您指定方向(此处为纵向)中的选项卡?

我还阅读了这篇文章iOS 6 UITabBarController 支持当前 UINavigation 控制器的方向,但正如一个评论还提到“这几乎对我有用。问题是如果我在将标签切换到仅纵向视图时已经处于横向状态,它仍然存在在横向。旋转纵向修复它,它不会旋转回横向,但我仍然需要它在第一次加载时是纵向的“这在这里几乎是一样的..

4

1 回答 1

2

我自己也遇到了这个问题,我想出了一个解决方案,它不漂亮,但它有效。

在你的 TabbarController 子类中实现这个 tabbarcontroller 委托函数(记得设置委托):

-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    int selected = self.selectedIndex;
    UIViewController *con = [[UIViewController alloc] initWithNibName:@"XIBName" bundle:nil];
    [[self.viewControllers objectAtIndex:selected] pushViewController:con animated:NO];
    [[self.viewControllers objectAtIndex:selected]popViewControllerAnimated:NO];
    [[self.viewControllers objectAtIndex:selected] setDelegate:nil];
}

在标签导航控制器中的 uinavigationcontroller 上的推送和弹出将使标签栏控制器再次触发其方向功能,如果您正确实现了方向代码,它将更改为您想要的方向。

我希望这会有所帮助,如果我需要详细解释任何内容,请随时发表评论。

最好的问候莫腾

于 2012-11-30T13:27:34.550 回答