1

我很困惑在ios中执行自动转换。
我有一个UIViewController,我告诉它自动旋转里面shouldAutoRotateToInterfaceOrientation。但是,它不起作用。 因此,我阅读了一些关于如何查看导航堆栈和/或是否使用UITabBarController的信息,因为这已知会导致各种混淆(引发手动)。ios5中的自动转换 - 为什么会失败的最常见原因?

实际上,我有一个UITabBar和一个UINavigationController。我想旋转的UIView被深入三层或四层“层”。

为什么不自动旋转只能通过shouldAutoRotateToInterfaceOrientation在当前UIViewController内部返回来确定?

从位于苹果的技术文档:https://developer.apple.com/library/ios/#qa/qa2010/qa1688.html为什么一个UIViewController可能不会旋转,当你期待它:

所有在你的UITabBarController或UINavigationController的子视图控制器不上一个共同的方向一致组。

要确保你的所有子视图控制器正常旋转, 必须实现代表每个选项卡或导航级各 视图控制器shouldAutorotateToInterfaceOrientation。每个必须 在旋转发生的方向相同。也就是说,他们 都应该为相同的方向位置返回YES。

有人可以总结一下我们在使用UITabBars和UINavigationControllers时需要注意的事实吗?人们常常在哪里搞砸或什么是浑浊点?

如果您有UITabBar,UITabBarController或UINavigationController,则其所有子视图都需要具有相同的自转行为。但肯定你可以有一些儿童的观点旋转,其他人不旋转,对吗?

我怀疑失败的常见原因与理解响应者链不够好,因为它适用于自动转换。如果是这样,有人可以帮我解决这个问题吗?那么我可能会弄清楚“选择性自动旋转”。

要添加到此,其他帖子指出您必须继承UITabBarController并覆盖shouldAutorotateToInterfaceOrientation。通过UIViewControllers,当连接到的UITabBarController UINavigationController的,并提出了

回答

2

我想你希望他们都以同样的方式回应的原因是因为它让用户感到尴尬。如果您在可旋转的特殊视图上处于横向模式,则将该视图控制器弹出到导航控制器上的父级或点击不具有横向模式的选项卡时,这将会是一种奇怪的体验。

我认为那是推理。

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

使用此:

[[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 

} 
1

我2美分...

意见,不是静态的,而是DINAMIC。我的意思是你必须认为用户可能会在你的观点之间切换。如果它们中的一些旋转而另一些不旋转,那么你的图形用户界面应该是混乱的,至少在苹果看来是这样。

0

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

对于iOS 5和下面

实施类别来处理情况UINavigationControllers标签相关情况:

@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收益确定