【问题标题】:Tabbar controller with navigationcontrollers orientation ios 6带有导航控制器方向的标签栏控制器ios 6
【发布时间】:2012-11-29 09:06:43
【问题描述】:

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

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

子类UITabbarcontroller

- (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 supported orientation with current UINavigation controller,但正如一条评论中提到的那样,“这几乎对我有用。问题是,当我将标签切换到仅纵向视图时,如果我已经处于横向状态,它仍然处于横向状态。旋转纵向修复它,它不会旋转回横向,但我仍然需要它在第一次加载时是纵向的”,这在这里几乎是一样的..

【问题讨论】:

    标签: ios uinavigationcontroller ios6 uitabbarcontroller


    【解决方案1】:

    我自己也遇到过这个问题,我想出了一个解决方案,虽然不是很漂亮,但它确实有效。

    在你的 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];
    }
    

    在 tabs navigationcontroller 中的 uinavigationcontroller 上的 push 和 pop 将使 tabbarcontroller 再次触发其 Orientations 功能,如果您正确实现了方向代码,它将更改为您想要的方向。

    希望对你有帮助,如果我需要详细解释,请随时发表评论。

    最好的问候 莫腾

    【讨论】:

    • 嗯,它可能并不漂亮,但它可以作为一种魅力!即使您切换标签,我现在也可以将一个视图切换为横向,而其他视图则保持纵向!多次感谢您的帮助!正如你所说,设置委托很重要,我在 viewwillappear 在我的子类中并按预期工作!希望这对其他人也有帮助。
    • 嗯,这不是与 iOS 对添加到标签栏控制器的视图控制器所做的内部内存管理搞混了吗?我们遇到的问题是我们的整个应用程序已经被故事板化,这意味着我们不能在代码中初始化视图控制器。对于使用 Storyboard 的项目,有没有办法解决这个问题?
    • @jowie:您可以向情节提要添加一个空视图控制器,分配情节提要 ID,然后调用 [self.storyboard instantiateViewControllerWithIdentifier:@"EmptyVC"]。
    猜你喜欢
    • 2017-05-14
    • 1970-01-01
    • 2016-11-08
    • 2012-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-31
    • 1970-01-01
    相关资源
    最近更新 更多