【问题标题】:iOS 6 UITabBarController supported orientation with current UINavigation controlleriOS 6 UITabBarController 支持当前 UINavigation 控制器的方向
【发布时间】:2012-09-12 08:06:14
【问题描述】:

我有一个正在更新到 iOS 6 的 iPhone 应用程序,但该应用程序存在轮换问题。我有一个 UITabBarController 和 16 个 UINavigationCotrollers。大多数子视图可以纵向或横向工作,但其中一些仅是纵向的。在 iOS 6 中,事情在不应该发生的时候发生了变化。

我尝试子类化 tabBarController 以返回当前 navigationController 选择的 viewController 的 supportedInterfaceOrienations

- (NSUInteger)supportedInterfaceOrientations{

    UINavigationController *navController = (UINavigationController *)self.selectedViewController;
    return [navController.visibleViewController supportedInterfaceOrientations];
}

这让我更接近了。视图控制器在可见时不会旋转错位,但如果我处于横向并切换选项卡,新选项卡将处于横向,即使它不受支持。

理想情况下,应用程序将仅位于当前可见视图控制器支持的方向。有什么想法吗?

【问题讨论】:

    标签: uinavigationcontroller rotation uitabbarcontroller orientation ios6


    【解决方案1】:

    继承您的UITabBarController 覆盖这些方法:

    -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        // You do not need this method if you are not supporting earlier iOS Versions
        return [self.selectedViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
    }
    
    -(NSUInteger)supportedInterfaceOrientations
    {
        return [self.selectedViewController supportedInterfaceOrientations];
    }
    
    -(BOOL)shouldAutorotate
    {
        return YES;
    }
    

    继承你的UINavigationController 覆盖这些方法:

    -(NSUInteger)supportedInterfaceOrientations
    {
        return [self.topViewController supportedInterfaceOrientations];
    }
    
    -(BOOL)shouldAutorotate
    {
        return YES;
    }
    

    然后在你不想旋转的 viewControllers 中实现这些方法:

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }
    
    -(BOOL)shouldAutorotate
    {
        return NO;
    }
    
    -(NSUInteger)supportedInterfaceOrientations
    {
        return UIInterfaceOrientationMaskPortrait;
    }
    

    对于您确实想要旋转的视图控制器:

        - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
        {
            return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
        }
    
        -(NSUInteger)supportedInterfaceOrientations
        {
            return UIInterfaceOrientationMaskAllButUpsideDown;
        }
    
        -(BOOL)shouldAutorotate
        {
            return YES;
        }
    

    您的 tabbarController 应该添加为应用程序窗口的 RootviewController。如果您计划支持默认方向,iPhone 的默认方向是颠倒,那么您不需要做任何其他事情。如果您想支持颠倒或不想支持其他方向,则需要在 app delegate 和/或 info.plist 中设置适当的值。

    【讨论】:

    • 这几乎对我有用。问题是,当我将标签切换到纵向视图时,如果我已经处于横向状态,它仍然处于横向状态。旋转纵向修复它,它不会旋转回横向,但我仍然需要它在第一次加载时是纵向的。
    • 我不确定你需要做什么来旋转它,但我敢打赌你是在 -(void)viewWillLayoutSubviews 中做的。从记忆中我可能不完全正确地使用该方法名称。我自己使用此代码的视图在旋转时会完全改变,我使用该方法将它们重新配置回纵向模式。你也可以在 -viewWillDisappear 中尝试一些东西。也许 [self.view setNeedsDisplay]。我目前不在 Xcode 工作,所以这些只是我会在你的案例中探索的想法。
    • 几乎是我需要的。我仍然有一些问题。在 iphone 中,使用您的代码一切正常。我希望 ipad UI 只能是横向的。牢记 ios 6 和 4.3 如何实现这一目标?
    • 我并不是说这个答案会起作用,但我们不应该需要它。根据 Apple 的文档,它应该可以开箱即用。 developer.apple.com/library/ios/documentation/WindowsViews/…
    • @Ryan 你解决了标签切换旋转问题吗?我现在正在努力解决它:(
    【解决方案2】:

    我遇到了导航堆栈中的一些视图控制器支持所有方向的问题,有些只支持纵向,但UINavigationController 正在返回所有应用程序支持的方向,这个小技巧帮助了我。我不确定这是预期的行为还是什么

    @implementation UINavigationController (iOS6OrientationFix)
    
    -(NSUInteger) supportedInterfaceOrientations {
        return [self.topViewController supportedInterfaceOrientations];
    }
    
    @end
    

    【讨论】:

    • 我不明白这种技巧如何帮助您在一个导航控制器的不同视图控制器上具有不同的行为。据我所知,这个 hack 只会执行一次,所以所有视图控制器都将具有与 topViewController 相同的旋转行为。我错过了什么吗??
    • 每次设备更改方向时都会执行此代码,因此这将返回当前 uiviewcontroller 当前处于活动状态且可见的受支持方向
    【解决方案3】:

    我认为这样的东西更好(作为类别方法)

    -(NSUInteger) supportedInterfaceOrientations {
        if([self.topViewController respondsToSelector:@selector(supportedInterfaceOrientations)])
        {
            return [self.topViewController supportedInterfaceOrientations];
        }
        return UIInterfaceOrientationMaskPortrait;
    }
    

    这确保了方法的实现。如果你没有做这个检查并且方法没有实现(比如在 iOS5 环境中),应用程序应该会崩溃!

    【讨论】:

      【解决方案4】:

      如果您打算为所有视图控制器启用或禁用旋转,则不需要子类化 UINavigationController。 而是使用:

         -(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window 
      

      在你的AppDelegate

      如果您计划在您的应用中支持所有方向,但在父视图控制器上支持不同方向(例如UINavigationController 堆栈),您应该使用

         -(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window 
      

      来自AppDelegate 结合您的父视图控制器中的以下方法。

          - (BOOL)shouldAutorotate
      

      - (NSUInteger)supportedInterfaceOrientations
      

      但是如果您计划在同一个导航堆栈中的不同 CHILDREN ViewControllers 中有不同的方向设置(像我一样),您需要检查导航堆栈中的当前 ViewController。

      我在 UINavigationController 子类中创建了以下内容:

          - (BOOL)shouldAutorotate
      {
          return YES;
      }
      
      - (NSUInteger)supportedInterfaceOrientations
      {
          int interfaceOrientation = 0;
      
          if (self.viewControllers.count > 0)
          {
              DLog(@"%@", self.viewControllers);
              for (id viewController in self.viewControllers)
              {
                  if ([viewController isKindOfClass:([InitialUseViewController class])])
                  {
                       interfaceOrientation = UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
                  }
                  else if ([viewController isKindOfClass:([MainViewController class])])
                  {
                       interfaceOrientation = UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
                  }
                  else
                  {
                       interfaceOrientation = UIInterfaceOrientationMaskAllButUpsideDown;
                  }
              }
          }
          return interfaceOrientation;
      }
      

      由于您无法再从子 ViewControllers 控制呈现的视图控制器的旋转设置,您必须以某种方式拦截当前在导航堆栈中的视图控制器。所以这就是我所做的:)。希望对您有所帮助!

      【讨论】:

        猜你喜欢
        • 2012-11-25
        • 2019-01-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多