ios5中的自动旋转——它失败的最常见原因?

ios5中的自动旋转——它失败的最常见原因?,ios,uinavigationcontroller,uitabbarcontroller,orientation,autorotate,Ios,Uinavigationcontroller,Uitabbarcontroller,Orientation,Autorotate,我对ios中自动旋转的实现感到困惑。 我有一个UIViewController,我告诉它在shouldAutoRotateToInterfaceOrientation内自动旋转。但是,它不起作用。 因此,我阅读了一些关于我需要如何查看导航堆栈和/或是否使用了UITabBarController的内容,因为众所周知,这会引起各种各样的混淆(举手) 事实上,我有一个UITabBar和一个UINavigationController。我要旋转的UIView被推到堆栈的三到四层 为什么自动旋转不完全由当

我对ios中自动旋转的实现感到困惑。
我有一个UIViewController,我告诉它在shouldAutoRotateToInterfaceOrientation内自动旋转。但是,它不起作用。 因此,我阅读了一些关于我需要如何查看导航堆栈和/或是否使用了UITabBarController的内容,因为众所周知,这会引起各种各样的混淆(举手)

事实上,我有一个UITabBar和一个UINavigationController。我要旋转的UIView被推到堆栈的三到四层

为什么自动旋转不完全由当前UIViewController中shouldAutoRotateToInterfaceOrientation返回的内容决定

苹果的技术文档位于:关于为什么UIViewController在您预期的情况下可能不旋转:

UITabBarController或UINavigationController中的所有子视图控制器在公共方向集上不一致

要确保所有子视图控制器都正确旋转, 您必须为每个应用程序实现shouldAutorotateToInterfaceOrientation 表示每个选项卡或导航级别的视图控制器。每个人都必须 在旋转发生的相同方向上达成一致。就是他们, 对于相同的方向位置,所有选项都应返回“是”

有人能总结一下我们在使用UITabbar和UINavigationController时需要注意的事实吗?人们通常在哪里搞砸了,或者什么是肮脏的地方

如果您有UITabBar、UITabBarController或UINavigationController,则其所有子视图都需要具有相同的自动旋转行为。但肯定可以让一些子视图旋转,而另一些不旋转,对吗

我怀疑失败的常见原因与没有充分理解应答器链有关,因为它适用于自动旋转。如果是这样,有人能帮我澄清一下吗?然后我也许能算出“选择性自转”

为了补充这一点,其他帖子指出,您必须将UITabBarController子类化,并覆盖shouldAutorotateToInterfaceOrientation。

我的2美分


当连接到UIAbbarController和UINavigationController时,UIViewController显示的视图不是静态的,而是动态的。我的意思是,你必须认为用户可能会在你的视图之间切换。如果其中一些是旋转的,而另一些不是旋转的,那么你的GUI应该是混乱的,至少在苹果看来是这样。

我认为你希望它们都以同样的方式响应的原因是,这会让用户感到尴尬。如果您在可以旋转的特殊视图上处于横向模式,则当您在navigationcontroller上将该viewcontroller弹出到父视图或点击没有横向模式的选项卡时,将是一种奇怪的体验

我认为这就是原因所在

但是,如果需要,您可以捕获设备方向通知并自行处理,如果设备在要旋转的特定视图上旋转,则可以推入新视图

使用以下命令:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(rotationDetected) name:@"UIDeviceOrientationDidChangeNotification" object:nil];
然后创建一个函数
rotationDetected
,处理旋转时发生的情况…如下所示:

-(void) rotationDetected {

    if (([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft) || ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight)){

    //Push on the view in landscape orientation if we aren't there already

} else if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait || [[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown) {

    //If the landscape view is present, pop it back to the portait view  

}

我遇到过同样的问题,希望这个解决方案能帮助其他人

适用于iOS 5及以下版本

实施分类以处理与选项卡关联的UINavigationController的情况:

@implementation UITabBarController (AutoRotation)

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

 {
          if ([self.selectedViewController isKindOfClass:[UINavigationController class]]) 

          {
              UIViewController *rootController = [((UINavigationController *)self.selectedViewController).viewControllers lastObject];
             return [rootController shouldAutorotateToInterfaceOrientation:interfaceOrientation];

          }
         return [self.selectedViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];

  }

@end
现在,一旦实现了该类别,自动旋转将完全由当前UIViewController中的shouldAutoRotateToInterfaceOrientation返回的内容决定