iPhone屏幕随机旋转?

时间:2010-03-14 10:24:18

标签: iphone uitabbarcontroller rotation

我使用tabBar Controller作为根控制器。它有4个选项卡,每个ViewControllers都有

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

以及tabBarController本身。 但是当我旋转设备(真实或模拟器)时,屏幕随机转动!如果在我打开应用程序时它没有转动它将具有相同的行为,直到我退出应用程序。 我试图在IB中逐个添加4个viewControllers,看看是否有问题,但我得到了同样的问题。只有在根本没有标签时它才会转动!

如果您有任何想法,请告诉我。谢谢!

4 个答案:

答案 0 :(得分:1)

您可以将每个视图控制器设置为响应任何可能的方向。因此,每个视图都会尝试旋转到每个方向。

视图不会真正自动旋转。除了最简单的视图外,您通常必须以编程方式管理子视图的位置。

如果您没有自定义方向代码,您可能会看到视图尝试在横向框架中绘制纵向视图,反之亦然。如果您设置了autoresize subviews,则子视图将以看似随机的模式显示在屏幕上。您更改方向越多,放置就越随机。

对于复杂视图,我喜欢为每个方向创建单独的viewController / view对。然后我将视图放在导航控制器中。随着方向的改变,每个视图控制器将推动或弹出适当的视图控制器,以便将进入的方向放入/移出堆栈。对于用户来说,这看起来像是一个优雅地重绘自己的视图。 (如果您具有必须通过变换手动旋转的非标准UI元素,则此功能特别有用)

答案 1 :(得分:0)

您必须继承UITabBarController并实施shouldAutorotateToInterfaceOrientation:

答案 2 :(得分:0)

实际上,我只想让我的第一个标签视图控制器旋转。所以我把这个代码放在我的自定义tabBarController:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
        if (self.selectedIndex == 0) {
            return toInterfaceOrientation != UIInterfaceOrientationPortraitUpsideDown;
        }else {
            return toInterfaceOrientation == UIInterfaceOrientationPortrait;
        }
}

但我遇到了同样的问题。当转向横向时,我为我的第一个标签视图控制器使用自定义方向代码。在我的自定义tabBarcontroller中使用以下函数调用:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    if (toInterfaceOrientation == UIInterfaceOrientationPortrait) {
        //rotation to Portrait
        lastOrientation = toInterfaceOrientation;
        [[UIApplication sharedApplication] setStatusBarHidden:NO animated:NO];
        [self.selectedViewController willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
    }
    else if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {
        if (!UIInterfaceOrientationIsLandscape(lastOrientation)) {
            //rotation to Landscape
            [self.selectedViewController willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
        }
        lastOrientation = toInterfaceOrientation;
    }
}

答案 3 :(得分:0)

我发现如果以编程方式设置选定的选项卡,tabViewController会不规则地旋转。

相关问题