【问题标题】:Supporting both iOS 6 and iOS 5 autorotation支持 iOS 6 和 iOS 5 自动旋转
【发布时间】:2012-09-08 05:41:07
【问题描述】:

谁能确认要同时支持 iOS 6 和 iOS 5,添加新的 iOS 6 自动旋转方法是没有意义的,因为 Apple 文档建议如果您还实现 iOS 5 方法,这些方法将被完全忽略?

特别是,我讨论了- (NSUInteger)supportedInterfaceOrientations- (BOOL) shouldAutorotate 方法——如果您还实现了- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation,编译器会忽略和合成这些方法

【问题讨论】:

    标签: iphone objective-c ios uiviewcontroller uikit


    【解决方案1】:

    如果您将应用程序打包到新的 sdk 中,则需要为自动旋转添加新的回调。但是,只有在 iOS 6 设备上运行此类应用程序时才会收到这些回调。对于在早期 iOS 版本上运行的设备,将收到较早的回调。如果您不实现新的回调,则默认行为是您的应用程序在 iPad 上以所有方向运行,在 iPhone 上除 UpsideDown 方向之外的所有方向运行。

    【讨论】:

      【解决方案2】:

      适用于 iOS5 旋转。 检查您想要的方向并返回 YES

      - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
      {  
          if ((interfaceOrientation==UIInterfaceOrientationPortrait)||(interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown)) {
              return YES;
          }
          else return NO;
      }
      

      iOS 6.0 自动旋转支持

      - (BOOL)shouldAutorotate
      {
          return NO;
      }
      
      - (NSUInteger)supportedInterfaceOrientations
      {
          return UIInterfaceOrientationIsPortrait(UIInterfaceOrientationMaskPortrait|| UIInterfaceOrientationMaskPortraitUpsideDown);
      
      }
      
      - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
      {
          return UIInterfaceOrientationIsPortrait(UIInterfaceOrientationPortrait|| UIInterfaceOrientationPortraitUpsideDown);
      
      }
      

      【讨论】:

      • 在iOS 6代码上,supportedInterfaceOrientations不应该只返回UIInterfaceOrientationMaskPortrait吗? preferredInterfaceOrientationForPresentation 不应该只返回UIInterfaceOrientationPortrait 吗?上面的 iOS 6 代码对我来说不合适。
      • @Rob:我只是建议方法,请返回你自己想要的方向。如果你不想要 UIInterfaceOrientationPortraitUpsideDown 你可以只返回 UIInterfaceOrientationPortrait。
      • 我同意这些是应该实现的方法这一事实,但我只是指出您对这些方法的实现是不正确的(不要使用返回布尔值的UIInterfaceOrientationIsPortrait,在这些方法中)。 supportedInterfaceOrientations 应该返回按位掩码值,而不是布尔值。同样,preferredInterfaceOrientationForDevice 应该返回单个 UIInterfaceOrientation 值,而不是布尔值。
      • 顺便说一句,我很抱歉,因为我继续编辑你的答案,但未经你的许可我真的不应该这样做,所以我恢复了你的原始代码。
      【解决方案3】:

      我认为最优雅的解决方案是:

      - (NSUInteger)supportedInterfaceOrientations {
          return UIInterfaceOrientationMaskPortrait;
      }
      
      - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
          return ((1 << toInterfaceOrientation) & self.supportedInterfaceOrientations) != 0;
      }
      

      我错过了什么吗?

      【讨论】:

      • 或只是return ((1 &lt;&lt; toInterfaceOrientation) &amp; self.supportedInterfaceOrientations) != 0;
      • @danskeel 您的回答对原始问题有何帮助:支持 both iOS 5 和 6?在 iOS 5 中调用 supportedInterfaceOrientations 会导致崩溃,因为它是 iOS 6 的一项功能。 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MYMainMenuTableViewController supportedInterfaceOrientations]: unrecognized selector sent to instance 0x68649f0'
      • 您是否添加了我提到的两种方法?我猜您忘记将第一个添加到您的班级MYMainMenuTableViewController。这不是问题,因为它只是一种方法,我可以自己调用它。
      • 代码异味。见Primitive Obsession
      • 什么?为什么?不要得到连接......你的意思是NSUinteger?如果你愿意,对苹果说,而不是对我说。 supportedInterfaceOrientations
      【解决方案4】:
       - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
      {
          return UIInterfaceOrientationMaskAll;
      }
      
      -(void)viewWillLayoutSubviews
      {
      if([self interfaceOrientation] == UIInterfaceOrientationPortrait||[self interfaceOrientation] ==UIInterfaceOrientationPortraitUpsideDown)
          {
             if (screenBounds.size.height == 568)
              {
                //set the frames for 4"(IOS6) screen here
               }
              else
      
              {
               ////set the frames for 3.5"(IOS5/IOS6) screen here
              }
      
      }
              else if ([self interfaceOrientation] == UIInterfaceOrientationLandscapeLeft||[self interfaceOrientation] == UIInterfaceOrientationLandscapeRight)
              {
               if (screenBounds.size.height == 568)
              {
                //set the frames for 4"(IOS6) screen here
               }
              else
      
              {
               ////set the frames for 3.5"(IOS5/IOS6) screen here
              }
      
      
          }
      //it is for IOS5
       - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
          {
           return YES;
          }
      

      -(void)viewWillLayoutSubviews 这个方法会为 ios5/ios6 调用。此代码对 3.5" 屏幕的 ios6/ios5/ios6 和 ios6 的 4" 屏幕都很有用。

      【讨论】:

      • - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return UIInterfaceOrientationMaskAll; } 会导致崩溃。因为返回值应该是一个具体的方向而不是掩码
      【解决方案5】:

      您必须在某处设置一个标志(我相信您的 Info.plist),以指示您使用两者中的哪一个。如果你使用新的,你不能为 iOS 5 构建(或者至少,它不能在 iOS 5 上工作)。如果您使用旧方法,则不会调用新方法。所以是的,您几乎必须选择要使用的方法,如果您想支持 iOS 5,则不能使用新方法。

      【讨论】:

      • 关于旗帜,你确定吗?我在 iOS 6 发行说明中没有看到任何提及;他们只是声明如果旧方法被实施,新方法将被忽略。
      • 看看 WWDC Session 200,大约 26' 他们谈论这个。另请查看第 236 节,23'45" 以后。不确定这是否仍在 NDA 下,所以请直接查看
      • 新的 iOS 6 支持这两种回调。在使用新 sdk 打包并在 iOS 6 设备上运行的应用程序上会收到新的回调。对于所有其他情况,较早的回调很受欢迎。已经在许多情况下对此进行了测试。
      • 100% 同意@vabhatia ,如果您实施相关方法,它适用于 iOS 5 和 6 设备
      【解决方案6】:

      为了在 ios5 和 ios6 中支持自动旋转,我们需要在 ios6 的情况下提供回调......

      [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
      [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(orientationChanged:) 
          name:UIDeviceOrientationDidChangeNotification object:nil];
      

      我们需要打电话

      - (NSUInteger)supportedInterfaceOrientations {
      return UIInterfaceOrientationMaskPortrait;
      
      } 
      
      -(BOOL)shouldAutoRotate{
          return YES;
        }
      

      ios5

      - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
      {
      return ((toInterfaceOrientation == UIInterfaceOrientationPortrait) || (toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown));
      }
      

      【讨论】:

      • 我认为没有必要对回调进行硬编码,如果您使用 UIKit 并且正确设置了项目设置,它们在 iOS 中是自动的。
      猜你喜欢
      • 2012-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多