【问题标题】:iPhone iOS 6 UITabBarController Portrait Only, How to have Modal View Controller support Landscape OrientationiPhone iOS 6 UITabBarController 仅纵向,如何让模态视图控制器支持横向方向
【发布时间】:2012-11-25 18:38:02
【问题描述】:

我想做的是最简单的概念。但是,我只是没有得到任何想要的结果。

我的应用是标准的 Tab Bar 应用,每个选项卡中的所有视图控制器都只支持纵向,这正是我想要的。

但是,在应用程序的一个部分中,我显示了一个模态视图控制器,它显然覆盖了标签栏控制器。这是一个文本输入屏幕,我非常希望这个视图能够支持横向和纵向。然后,一旦用户取消了该模态视图控制器,标签栏控制器将再次显示,一切都是纵向的。

我尝试了很多东西,但没有任何效果。如果我告诉应用程序支持两种方向,那么旋转会在模态上正确发生,但也会在应用程序的其余部分发生,这是我不想要的。

我已经尝试实现所有新的 shouldAutorotate 和 supportInterfaceOrientations 方法,但似乎没有任何效果。

我几乎要工作的最接近的尝试是我在我的应用程序委托中创建了一个 UITabBarController 类别,以转发 shouldAutorotate 和supportedInterfaceOrientations。这最初似乎可行,但由于某种原因,每当取消我的模态 vc 时,我的应用程序标签栏部分总是在状态栏后面向上移动 20 像素?我不知道那是什么。

我创建了一个测试应用程序,其中没有 UITabBarController,我能够毫无问题地编写我想要的行为,并且它运行良好。因此,很明显,Tab Bar Controller 方面的某些东西使这成为一个难题。

请告诉我解决这个简单概念的诀窍是什么。

谢谢!

【问题讨论】:

    标签: iphone ios sdk uitabbarcontroller landscape


    【解决方案1】:

    显然在 ios6 及更高版本中,旋转的工作方式是不同的。所以你要做的就是以下

    1. 在您的 .plist 中支持所有 4 个方向。
    2. 子类化 UITabBarController(例如:CustomTabBarController)
    3. 在 CustomTabBarController 中放入以下代码行

      -(NSUInteger)supportedInterfaceOrientations
      {
          return UIInterfaceOrientationMaskPortrait;
      }
      
      
      - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
      {
          return UIInterfaceOrientationPortrait;
      }
      
    4. 在您的应用委托或您正在初始化 UITabBarController 的任何位置,将这些实例替换为 CustomTabBarController 实例。

    5. 在您的模态控制器中放置线条

      - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
       {
          return UIInterfaceOrientationLandscapeLeft;
      }
      
      -(BOOL)shouldAutorotate{
          return NO;
      
      }
      

    它应该都可以工作。

    显然,我发现的诀窍是,UITabBarController 不会听你的指令。它将支持您在 .plist 中提到的所有方向。

    因此你必须继承它。

    我尝试了上述所有方法,并且效果很好。请告诉我,如果需要,我可以将代码发送给您。

    【讨论】:

    • 感谢您的输入,但是我能够提出使用类别的解决方案,并将其发布为答案。由于 SO 规则,我无法“接受”我的回答 2 天。
    • 这很完美!需要注意的一件事。如果您使用故事板,则必须将选项卡栏控制器上的自定义类更改为您创建的自定义类。
    【解决方案2】:

    我能够通过为 UITabBarController 和 UINavigationController 创建几个类别来解决这个问题。这是我使用的代码:

    @implementation UITabBarController (rotations)
    
    - (BOOL)shouldAutorotate
    {
        return [self.selectedViewController shouldAutorotate];
    }
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
    {
        return [self.selectedViewController shouldAutorotateToInterfaceOrientation:toInterfaceOrientation];
    }
    
    - (NSUInteger)supportedInterfaceOrientations
    {
        return [self.selectedViewController supportedInterfaceOrientations];
    }
    
    @end
    
    @implementation UINavigationController (navrotations)
    
    - (BOOL)shouldAutorotate {
    
        return [self.topViewController shouldAutorotate];
    }
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
    {
        return [self.topViewController shouldAutorotateToInterfaceOrientation:toInterfaceOrientation];
    }
    
    - (NSUInteger)supportedInterfaceOrientations
    {
        return [self.topViewController supportedInterfaceOrientations];
    }
    
    @end
    

    然后,当然,我显示的每个视图控制器都需要响应 shouldAutorotate 和 supportedInterfaceOrientations 方法。

    【讨论】:

      猜你喜欢
      • 2012-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      • 2012-09-12
      • 2012-12-21
      • 1970-01-01
      相关资源
      最近更新 更多