【问题标题】:Interface orientation in iOS6 and iOS 5 on iPad is not workingiPad 上 iOS6 和 iOS 5 中的界面方向不起作用
【发布时间】:2012-12-17 09:02:26
【问题描述】:

我正在创建一个支持 ios 版本 5 和 6 的 iPad 应用程序。我有一个视图控制器,它仅在应用程序开始时以纵向模式显示。后来我有了一个支持所有界面方向的视图控制器。

我只是让它在 iOS 5 或 iOS 6 上工作。含义:在 iOS 6 上,视图控制器中支持的界面方向只有在 info.plist 文件中也设置时才有效。所以我必须在 info.plist 文件中设置所有界面方向。这一切都适用于 iOS 6,但在 iOS 5 上,如果 iPad 在应用程序启动之前处于纵向状态,则它适用于启动。但是,如果 iPad 处于横向模式,则应用程序以正确的方式启动,但通知中心是错误的,直到您手动执行另一个轮换。

有没有办法将一些其他键放入另一个 iOS 版本的 info.plist 文件中?这是我能想到的唯一解决方案。因为如果我在 info.plist 文件中仅将纵向设置为方向,则它适用于 iOS 5。

编辑:

好的,我解释的有点混乱。我已经通过以下方法让它在 iOS 6 中完全工作:shouldAutorotate 和supportedInterfaceOrientations:。我的根视图控制器只支持纵向界面方向,在我的 info.plist 中我必须选择所有界面方向。

问题:

当我在 iOS 5 上启动应用时,我的 iPad 处于横向状态:

根视图控制器处于纵向模式,但应用程序的行为就像处于横向模式,这意味着可以从侧面访问通知中心等等。

我能想到的解决方案:

问题是我必须在 info.plist 文件中设置所有界面方向。这意味着在 iOS 5 中,即使我只是在方法 shouldAutorotateToInterfaceOrientation: 中放置了一个可能的方向,应用程序也会尝试以横向模式启动(上述行为已解释)。

所以会有两种解决方案:当在 info.plist 文件中未选择特定界面方向时,我可以让应用在 iOS 6 中旋转,或者我可以“更改”iOS 5 的旋转。

希望你能帮助我。

【问题讨论】:

    标签: objective-c xcode ipad cocoa-touch


    【解决方案1】:

    好的,最后我想出了一个解决方案。问题是由于 info.plist 文件中设置的界面方向,在启动时以错误的方式访问了通知中心。

    我有一个全屏应用程序,一开始并没有得到它,状态栏也以横向方式显示,而其他视图以纵向方式显示(rootviewcontroller)。

    所以在应用程序:didFinishLaunchingWithOptions:我把这个代码:

    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
    

    对于所有希望在启动时拥有一个具有一个方向的应用程序但又允许另一个子控制器中的多个应用程序的所有其他人,这里是完整的解决方案:

    在启动时为 iPad 设置所有界面方向。

    在我的 rootViewController 中有这个代码:

    (支持旧 iOS 版本)

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

    (支持新的)

    - (NSUInteger)supportedInterfaceOrientations {
        return UIInterfaceOrientationMaskPortrait;
    }
    
    - (BOOL)shouldAutorotate {
            return NO;
    }
    

    在我的子控制器中,我有以下内容:

    - (NSUInteger)supportedInterfaceOrientations {
        return UIInterfaceOrientationMaskAll;
    }
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
        return YES;
    }
    
    - (BOOL)shouldAutorotate {
        return YES;
    }
    

    看起来很简单,但我花了很长时间才用状态栏解决这个问题。如果 shouldAutorotate 没有被调用,你必须使用其他帖子中提到的这个技巧。

    【讨论】:

      【解决方案2】:

      对于 iOS 6,您需要在 UIViewControllers 中重写这两个方法:

      - (NSUInteger)supportedInterfaceOrientations
      - (BOOL)shouldAutorotate
      

      此外,Info.plist 方向仅用于启动,因此它们应与您的启动方向相匹配,即仅纵向。

      注意自动旋转只有在正确使用 UIViewcontrollers 时才能正常工作,例如 UINavigationController 中的堆栈。这样,当显示每个控制器时,它将使用其指定的方向。

      【讨论】:

      • 我不是说它在 iOS 6 中可以正常工作吗?我的问题是我必须激活所有界面方向 info.plist 文件。但如果我这样做,我就会遇到上面提到的问题。
      • 您是否使用supportedInterfaceOrientations 和shouldAutorotate?这就是它在 iOS 6 中的工作方式。 Info.plist 用于启动。我从文档中引用:“系统使用此信息(以及当前设备方向)来选择启动应用程序的初始方向。”
      • 好吧,太晚了,我编辑我的问题,会更准确。
      【解决方案3】:

      这是我想出的向后兼容的方法。

      - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
          if ([CMVStaticContainer isPad]) // Custom Method to determine this is on the iPad
          {
              return UIInterfaceOrientationLandscapeRight;
          }
          else
          {       
              return UIInterfaceOrientationPortrait;
          }
      }
      
      - (NSUInteger)supportedInterfaceOrientations{
          return UIInterfaceOrientationMaskAll;
      }
      
      - (BOOL)shouldAutorotate{
          UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
          if (orientation == UIDeviceOrientationUnknown) return YES;
          BOOL result = [self shouldAutorotateToInterfaceOrientation:orientation];
          return result;
      }
      
      - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
          // Return YES for supported orientations
          if ([CMVStaticContainer isPad]) // Custom Method to determine this is on the iPad
          {
              return interfaceOrientation == UIInterfaceOrientationLandscapeRight;
          }
          else
          {       
              return YES;
          }
      
      }
      

      我确定对于 UIDeviceOrientationUnknown,您应该始终返回 yes。然后我的代码将它转发到 iOS 5 中的另一个方向代码

      哦,我还遇到了将“标准类”作为 rootViewController 的问题,因为这是获取消息的那个,我相信我将它子类化并确定了位于顶部的视图控制器并通过它发送消息以确保顶部的视图控制器将确定首选方向以及 shouldAutorotate 命令。

      此代码不包含该代码,但那是因为我将模态表示用于新的视图控制器,而不是 UINavigationController 用于我的根。但这是一个无关的决定。

      【讨论】:

      • 如果在 info.plist 文件中没有选择特定的界面方向,这是否有效?
      • 您应该启用您的应用支持的所有方向。我相信在 ios 5 中,这些选择会设置默认值。但它们可以被覆盖。在 ios 6 中,您需要启用任何您将支持的功能,然后您可以使用上面显示的代码进行限制。
      • 好的,我在下面发布了一个解决方案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-23
      • 1970-01-01
      • 1970-01-01
      • 2012-03-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多