2010-03-14 176 views
0

我使用tabBar控制器作为根控制器。它有4个选项卡,其每个ViewControllers都有iPhone屏幕随机旋转?

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

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

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

回答

0

我发现,如果你设置所选标签编程的tabViewController不正常旋转。

0

你必须继承UITabBarController和实施shouldAutorotateToInterfaceOrientation:

+0

我目前的tabBarController目前是UITabBarController的一个子类,我已经实现了这个功能。 – Baldoph 2010-03-14 14:59:08

1

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

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

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

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

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; 
    } 
}