【问题标题】:iOS 6 Rotation issue - No rotation from Presented Modal View ControlleriOS 6 旋转问题 - 呈现的模态视图控制器没有旋转
【发布时间】:2012-09-15 06:34:08
【问题描述】:

我有一个 MainViewController,它有一个按钮,可以通过水平翻转来推送一个新视图 (InfoViewController)。像这样:

controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];

MainView 控制器支持 Portrait 和 PortraitUpsideDown。像这样:

- (BOOL)shouldAutorotate {
    return YES;    
}

- (NSUInteger)supportedInterfaceOrientations {    
    return (UIInterfaceOrientationMaskPortrait | 
            UIInterfaceOrientationMaskPortraitUpsideDown);
}

在我的 InfoViewController 中,它还说明了上述代码。在我的 AppDelegate 中,它在 LaunchOptions 中有这个:

[self.window setRootViewController:self.mainViewController];

在我的 app.plist 文件中,它支持所有方向。这是因为其他视图也需要支持横向。所以在我的 MainViewController 和 InfoViewController 上,我只需要 Portrait 和 PortraitUpsideDown。但从另一个角度来看,我需要所有的 orintations。

我的 MainViewController 工作正常,但我的 InfoViewController 适用于所有方向。

我很难让它在 iOS6 中工作。我研究了其他帖子并尝试了其他人提供的帮助,但没有任何运气。请有人能帮我实现这个谢谢。我是一个 Objective-C 新手 :p

【问题讨论】:

  • 在标题中写着“不旋转”。在你的倒数第二段中,它说它适用于所有方向。是哪一个?

标签: objective-c ios ios6


【解决方案1】:

不支持应用 plist 文件中的所有方向,仅支持根视图控制器支持的方向。

自动旋转在 iOS 6 中发生了变化。在 iOS 6 中,UIViewController 的 shouldAutorotateToInterfaceOrientation: 方法已被弃用。取而代之的是,您应该使用 supportedInterfaceOrientationsForWindow:shouldAutorotate 方法:

- (BOOL)shouldAutorotate {
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAllButUpsideDown;    
}

Modal ViewControllers 在 iOS 6 中不再获得旋转调用: willRotateToInterfaceOrientation:duration:, willAnimateRotationToInterfaceOrientation:duration:,didRotateFromInterfaceOrientation: 方法不再在任何进行全屏演示的视图控制器上调用 本身——例如那些被调用的:presentViewController:animated:completion:

您可以让呈现模态视图控制器的视图控制器通知它旋转。 此外,现在您使用:presentViewController:animated:completion: 来呈现视图控制器。 presentModalViewController:animated: 已弃用,您在代码中使用它。

【讨论】:

  • 好的,好的。我已经从 plist 中删除了除纵向(主页按钮底部)之外的所有方向。但现在我有一个需要所有方向的观点。我已经放了supportedInterfaceOrientations,然后列出了所有,但它不起作用。谢谢。
  • 如果你没有 shouldAutorotate 返回 YES,那么你的 supportedInterfaceOrientations 永远不会被调用并且视图不会旋转。你说的不起作用具体是什么意思?
  • 该视图的 shouldAutorotate 返回 YES。
  • 然后返回要允许旋转的supportedInterfaceOrientations。
  • 模态视图控制器在 iOS 6 中不再获得旋转调用
【解决方案2】:

我在使用标签栏控制器时解决了类似的问题。

子类 UITabBarController。实现这些方法:

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAll;
}


- (BOOL)shouldAutorotate
{
    NSLog(@"Orientation:%d", [[UIDevice currentDevice] orientation]);

    for (UIViewController *viewController in self.viewControllers) {
        [viewController shouldAutorotate];
    }

    return YES;
}

如果您想在 tabbarcontroller 中处理控制器的旋转,请在 tab bar 控制器中的每个控制器中也实现这些方法并编写代码来处理方向更改。如果您不想处理它,那么您不需要实现这些方法。 TabBarControllers 方法将始终在方向更改时运行。甚至两次,原因不明。

是的,不要忘记删除所有 shouldAutorotate 方法。我完全转向了新的定向模型。如果你想让它们留下来,可能会更难。

【讨论】:

    【解决方案3】:

    通过继承 UINavigationController 来创建一个类别并实现以下方法 在 .h 文件中

    -(BOOL)shouldAutorotate;
    -(NSUInteger)supportedInterfaceOrientations;
    - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation;
    
    
    
     in .m file
    
    -(BOOL)shouldAutorotate
    {
    return [self.topViewController shouldAutorotate];
    }
    
    -(NSUInteger)supportedInterfaceOrientations
     {
    return [self.topViewController supportedInterfaceOrientations];
     }
    
    -(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
     {
    return [self.topViewController preferredInterfaceOrientationForPresentation];
     } 
    

    并在视图控制器类中实现以下方法,你想要启用旋转的类

    -(NSUInteger)supportedInterfaceOrientations
    {
    return (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft |   UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown);
     }
    
    
    - (BOOL)shouldAutorotate
    {
    return YES;
    }
    - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
    {
       return UIInterfaceOrientationLandscapeLeft;
    }
    

    【讨论】:

      【解决方案4】:

      在子类 UITabBarController .m 上添加此代码

      @implementation UINavigationController (rotation)
      //temp hack for iOS6, this allows passing supportedInterfaceOrientations to child viewcontrollers.
      - (NSUInteger)supportedInterfaceOrientations {
          return [self.topViewController supportedInterfaceOrientations];
      }
      @end
      
      @implementation NameClassUITabBar
      
      - (void)viewDidLoad {
          [super viewDidLoad];
      }
      
      - (BOOL)shouldAutorotate
      {
          return YES;
      }
      
      - (NSUInteger)supportedInterfaceOrientations
      {
          return UIInterfaceOrientationMaskLandscape;
      }
      
      @end
      

      在这里,我在标签栏控制器中发布了我的解决方案/经验,并带有旋转: http://luterr.blogspot.sg/2015/04/example-code-uiinterfaceorientationmask.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-16
        • 1970-01-01
        相关资源
        最近更新 更多