iPhone屏幕随机旋转?

iPhone屏幕随机旋转?,iphone,uitabbarcontroller,rotation,Iphone,Uitabbarcontroller,Rotation,我使用tabBar控制器作为根控制器。它有4个选项卡,每个视图控制器都有 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { return YES; } 以及Tabbar控制器本身。 但当我旋转设备(真实或模拟器)时,屏幕会随机旋转!如果它在我打开应用程序时没有打开,它将具有相同的行为,直到我退出应用程序。 我试图在IB中逐个添加4个Vie

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

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
        return YES;
}
以及Tabbar控制器本身。 但当我旋转设备(真实或模拟器)时,屏幕会随机旋转!如果它在我打开应用程序时没有打开,它将具有相同的行为,直到我退出应用程序。 我试图在IB中逐个添加4个ViewController,以查看其中一个是否有问题,但我得到了相同的问题。它只有在完全没有标签的情况下才会转动


如果你有什么想法,请告诉我。谢谢

您必须子类化
UITabBarController
并实现
shouldAutorotateToInterfaceOrientation:

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

视图实际上不会自动旋转。除最简单的视图外,通常必须以编程方式管理子视图在所有视图中的位置

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


对于复杂视图,我喜欢为每个方向创建单独的viewController/视图对。然后我把视图放在导航控制器中。随着方向的更改,每个视图控制器将为即将到来的方向推送或弹出适当的视图控制器到堆栈上/下。对于用户来说,这看起来像是一个单独的视图正在优雅地重新绘制自己。(如果非标准UI元素必须通过变换手动旋转,这一点尤其有用)

实际上,我只想旋转我的第一个选项卡视图控制器。因此,我将此代码放在自定义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;
    }
}

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

我当前的tabBarController是UITabBarController的子类,我已经实现了此功能。