TabBar应用程序是否需要在其所有viewControllers上返回YESAutorotateToInterfaceOrientation?

时间:2011-12-06 17:50:33

标签: iphone ios uiviewcontroller uinavigationcontroller uitabbarcontroller

我正在使用TabBarController作为root控制器开发iphone应用程序。此根控制器与导航控制器相关联(每个控制器与特定的选项卡栏按钮相关)。

这些导航控制器上的一个显示了一些照片,因此我需要启用Lanscape,这显然不起作用,直到我将此代码添加到所有视图控制器

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

但是,现在所有方向都启用了所有方向,我在风景中不想要的一些视图显示非常难看:(

知道如何只在这张照片视图中保留景观取向并在所有其他观看中禁用它吗?

2 个答案:

答案 0 :(得分:3)

考虑以模态方式显示需要完全重定向功能的UIViewController。 对于处理这种情况,这是正常的,并且在我看来是正确的方式。

简短地回答您的主题:是的,如果您希望任何选项卡式viewControllers允许重新定位,则必须返回YES。对于UINavigationControllers堆栈中的viewControllers也是如此。因此,任何组合都是如此。

Apple关于该主题的技术说明:

  

为什么我的UIViewController不会随设备一起旋转?

     

UITabBarController中的所有子视图控制器   UINavigationController不同意共同的方向集。至   确保所有子视图控制器正确旋转,您   必须为每个视图实现shouldAutorotateToInterfaceOrientation   控制器代表每个选项卡或导航级别。每个人都必须同意   在旋转发生的相同方向上。也就是说,他们都是   对于相同的方向位置应该返回YES。

如果使用模态呈现的视图控制器不适合您 - 这是另一种方式......

还有一种解决方案似乎有点“hacky”,但过去对我有用。我将以非常“hacky”的方式起草它以简化答案。

在所有viewControllers的shouldAutorotateToInterfaceOrientation:toInterfaceOrientation实现中,返回一个全局值。根据当前显示的viewController的需要更改该全局值。即使有人说只有一次查看一个viewController,这种常见的谣言已被证明是不合理的。所以这就是超级黑客;

导航堆栈中的所有视图控制器都应该像这样实现shouldAutorotate方法:

- (BOOL)shouldAutorotateToInterfaceOrientation (UIInterfaceOrientation)toInterfaceOrientation
{
    extern BOOL gSouldAutorotateToAnyOrientationFlag;
    if (gShouldAutorotateToAnyOrientationFlag)
    {
        return YES;
    }
    return UIInterfaceOrientationIsPortrait(toInterfaceOrientation);   
}

现在,在你的应用程序的某个地方,你应该声明并实例化那个全局标志 - 你可以将这个丑陋的全局标志放在你的app-delegate实现中,直接位于导入之下和@implementation块之上:

BOOL gShouldAutorotateToAnyOrientationFlag = NO;

在应该仅以纵向模式显示的所有viewControllers中,将该标志设置为NO - 例如在viewWillAppear中;

- (void)viewWillAppear:(BOOL)animated
{
    extern BOOL gShouldAutorotateToAnyOrientationFlag;
    gShouldAutorotateToAnyOrientationFlag = NO;   
}

在viewController / s中应该以任何方向显示,将该标志设置为YES - 再次在viewWillAppear中;

- (void)viewWillAppear:(BOOL)animated
{
    extern BOOL gShouldAutorotateToAnyOrientationFlag;
    gShouldAutorotateToAnyOrientationFlag = YES;   
}

这样,每当要求整个导航堆栈的定位功能时,都会给出正确的答案。根据我的经验,一遍又一遍地询问整个堆栈,并且不会缓存答案,因此无论何时我需要它,我的黑客都会工作。尽管如此,谣言似乎仍然普遍存在,这些答案以某种方式被缓存,因此谣言在某些情况下可能是有效的 - 因此,如果这对您不起作用(或者甚至投票决定:D),我将不承担责任。

答案 1 :(得分:-1)

在视图中,您只想要横向显示:

- (BOOL)shouldAutorotateToInterfaceOrientation (UIInterfaceOrientation)toInterfaceOrientation
{
    return UIInterfaceOrientationIsLandscape(toInterfaceOrientation);
}

如果UIInterfaceOrientationIsLandscape更符合您的需求,您可以将UIInterfaceOrientationIsPortrait替换为{{1}}。