【问题标题】:UIViewController does not auto rotateUIViewController 不会自动旋转
【发布时间】:2011-01-25 20:44:37
【问题描述】:

正如标题所说。我的 UIViewController 无论如何都不会旋转。加载时会调用 shouldAutorotateToInterfaceOrientation,但之后不会调用。

更新 1:

这是一个非常奇怪的问题。至少对我来说。我会尽力解释一切。

这是一个基于导航的应用程序。每个控制器都有

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
    return YES; 
}

Xcontroller 是 Acontroller 的子对象,它不会自动旋转。如果 Xcontroller 成为 Bcontroller 的孩子,那么它将自动旋转。所以Acontroller有问题。但 Acontroller 与 Bcontroller 相同(数据除外)。

怎么了?

更新 2:

我决定重新创建 Acontroller。它奏效了。我相信我错过了一些愚蠢的东西。

【问题讨论】:

  • 你是如何实现-shouldAutorotateToInterfaceOrientation:的?
  • - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{ return YES;如果这就是你的意思:)。

标签: iphone uiviewcontroller


【解决方案1】:

我不确定这是否与您的情况相同。但我也经历过同样的事情。 shouldAutorotateToInterfaceOrientation 在开始时只被调用一次。 通过分解代码进行了一些认真的调试后,我发现原因在于我重写的 init 方法。 我以前有这个:

- (id)initWithAlbum:(PhotoAlbum *)theAlbum {
    if (self) {     
        self.photoAlbum = theAlbum;     
    }
    return self;
}

然后我改成了这个

- (id)initWithAlbum:(PhotoAlbum *)theAlbum {
    if (self = [super init]) {      
        self.photoAlbum = theAlbum;     
    }
    return self;
}

注意:唯一的区别是我添加了[super init] 来调用父级init。 进行此更改后,旋转效果很好,并且每次旋转屏幕时都会调用 shouldAutorotateToInterfaceOrientation 。 希望对您有所帮助。

【讨论】:

  • 嘿 DaiLak,你救了我的命。我遇到了和你一样的问题,我已经为此苦苦挣扎了好几个小时。谢谢。
【解决方案2】:

您的视图控制器不旋转可能有多种原因。

查看 Apple 关于此问题的官方问答:

为什么我的 UIViewController 不随设备旋转?

http://developer.apple.com/library/ios/#qa/qa2010/qa1688.html

【讨论】:

    【解决方案3】:

    Apple Q&A 有该问题的详细解决方案。

    为什么我的 UIViewController 不随设备旋转?

    http://developer.apple.com/library/ios/#qa/qa1688/_index.html

    如果你在uiwindow中添加一个viewcontroller.view,你应该把这个viewcontroller设置为rootviewcontroller。

    [self.window addSubview: mainViewcontroller.view];
    self.window.rootViewController=mainViewcontroller;
    

    【讨论】:

      【解决方案4】:

      另外,请确保您没有开启旋转锁定。我花了一个小时试图弄清楚为什么我的观点停止旋转。 shouldAutorotateToInterfaceOrientation 仅在启动时和出现 Game Center 排行榜/成就时被调用一次。

      【讨论】:

        【解决方案5】:

        我遇到了同样的问题 - 原因是,这是我在 ApplicationDelegate 中动态创建的第一个 UIViewController,将它的 View 添加到我的 UIWindow 并立即发布它。

        这当然是不正确的,因为我只是添加了 UIViewController 的 UIView(保留它)然后释放了整个控制器。

        您应该将您的第一个 UIViewController 作为实例变量添加到您的 ApplicationDelegate 中,然后在您的 ApplicationDelegate 的 dealloc 方法中释放它。

        【讨论】:

          【解决方案6】:

          在我的例子中,ViewController 位于 NavigationController 中,由接收方向更改的“父”viewControlled 使用。

          我对这个父母所做的是:

          - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
          
              if(_navigationController){
                  return  [_navigationController.topViewController shouldAutorotateToInterfaceOrientation: toInterfaceOrientation];
              }
          
              return toInterfaceOrientation == UIInterfaceOrientationPortrait;
          }
          

          通过这种方式,您可以根据当前可见的控制器实现自己的方向更改逻辑。

          【讨论】:

            【解决方案7】:
            • (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{ return YES; } 如果你使用上面的方法,你可以调用很多次,如果你想的话,你可以在没有任何错误的情况下调用。

            【讨论】:

            • 我正在使用这种方法。只有在控制器加载时才会调用它。
            【解决方案8】:

            我认为这里没有奇怪的行为,它只被称为正确的一种。不需要调用多个来决定设备是否应该向一个方向旋转。

            这个方法只是询问设备是否应该向一个方向旋转。 如果你想处理方向变化,你应该从 UIDeviceDidChangeOrientationNotification 注册通知并覆盖以下方法:

            - (void)orientationChanged:(NSNotification *)notification
            {
                UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
                if (UIDeviceOrientationIsLandscape(deviceOrientation) &&
                !isShowingLandscapeView)
                {
                    [self presentModalViewController:self.landscapeViewController
                                        animated:YES];
                    isShowingLandscapeView = YES;
                }
                else if (deviceOrientation == UIDeviceOrientationPortrait &&
                         isShowingLandscapeView)
                {
                    [self dismissModalViewControllerAnimated:YES];
                    isShowingLandscapeView = NO;
                }
            }
            

            查看更多here

            【讨论】:

            • 感谢您的回答。但我不明白为什么在我的其他控制器中 shouldAutorotateToInterfaceOrientation 足以旋转它们。而对于这个特定的控制器是不够的。同样在我的其他控制器中,每次我旋转设备时都会调用 shouldAutorotateToInterfaceOrientation。
            • 换句话说...我的控制器应该在每次旋转设备时调用 shouldAutorotateToInterfaceOrientation 以检查是否支持方向。好吧......我的“有问题”控制器仅在加载时调用它,而不是在设备旋转时调用它。
            • 设备旋转时,通过查询“shouldAutorotateToInterfaceOrientation:”询问控制器是否支持旋转,如果回答是,则调用willRotateToInterfaceDuration:duration:看是否支持需要做任何其他修改。
            • 我明白这一点。但在我的情况下,它只在控制器加载时询问。如果我旋转设备,它永远不会问。 Nvm 会重新创建这个控制器,看看这是否能修复它。
            • 正如我告诉你的,iphone 操作系统没有理由调用这个函数超过 1 次。所以你应该看看别处,看看你能做些什么来处理设备方向。
            【解决方案9】:

            我遇到了同样的问题,但是在应用程序的 UIWindow 中添加了两个视图控制器。原因 视图控制器的 UIView 属性嵌入在 UIWindow 中,但旁边还有一个额外的视图控制器

            来自 Apple 技术问答

            http://developer.apple.com/library/ios/#qa/qa1688/_index.html

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2011-08-16
              • 1970-01-01
              • 1970-01-01
              • 2013-03-24
              • 1970-01-01
              • 1970-01-01
              • 2011-11-18
              相关资源
              最近更新 更多