在iOS5/6中自动旋转?

问题描述:

我更新到Xcode 4.5,并与iOS6一起工作 - 我一定会错误而不是下次有更新;对于iOS这么新的人来说,这是一种噩梦 - 我刚刚注意到我正在开发的一个应用是自动旋转。在更新之前,我从来没有注意到它是自动调节,但也可能在测试时我没有旋转手机,所以我无法确定。我已经添加以下代码的主要的UIViewController它仍然旋转:在iOS5/6中自动旋转?

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

这是禁用自转的路吗?如果是这样,那么iOS6可能会发生一些变化,我将不得不等待完整版发现。但是如果我弄错了,应该用什么代码代替?

非常感谢您的帮助。

编辑:这是我改变它的代码,但它仍然在旋转。我错了吗?

​​

那是因为从来没有成功。你应该选择一种方向。

按住命令并单击UIInterfaceOrientation,您将看到可能选项的枚举。

然后你可以测试这些来决定你的YES情景。

我原本可能误解了你的问题。看起来你可能一直在说你的应用允许轮换。但代码应该禁止这样做。

我在想你是在说它还在开发代码。试图找到一个是有一点需要考虑的。是否可能有多个视图控制器可用。也许你的代码没有被击中。

有几个可能的问题。

  1. 你的代码甚至没有被使用。因为视图被分配为UIViewController而不是您的自定义视图控制器。

  2. 您正在使用的代码,但该视图控制器不是被问到方向的人。因此特定的代码没有被击中。

  3. 错误的构建不断将错误的程序集放到设备上。

您的解决方案可以如下。

  1. 确保您的代码是被分配的代码。要么你的自定义类有一个直接的分配。或者xib文件正在膨胀它。当您打开xib文件时,请查看Identity Inspector。选择您的视图控制器并确保将自定义类设置为您的类类型

  2. 查看层次结构。还有什么其他的视图控制器。也许其中一位正在告诉应用可以自动转向任何方向。

  3. 找到您的“DerivedData”文件夹并完全删除它。有时候这可以从组织者那里得到。其他时候你需要直接从磁盘上删除。然后清理并重新构建。

另一种解决方案可能很简单,就像在Project文件中设置设置一样。

从文件浏览器中选择您的项目文件,您将在摘要中看到iPad和iPod设置。您可以针对您想禁止的方向“取消按下”按钮。和任何视图控制器,否则你不会定位到方向。将默认使用这些。

我对此感到抱歉。

更新

我经常使用此代码来处理我的自转。

它不仅将ipad与其他ios设备区分开来,而且还将请求转发到呈现的控制器,以便显示模式的视图可以响应它的需求。

取向是一种痛苦,当你不明白:)

// Detect iPad 
#define IS_IPAD() ([[UIDevice currentDevice] respondsToSelector:@selector(userInterfaceIdiom)] ? \ 
[[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad : NO) 

// Set preferred orientation for initial display 
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{ 
    if (IS_IPAD()){ 
     return UIInterfaceOrientationLandscapeRight; 
    } 
    else { 
     return UIInterfaceOrientationPortrait; 
    } 
} 
// Return list of supported orientations. 
- (NSUInteger)supportedInterfaceOrientations{ 
    if (self.presentedViewController != nil){ 
     return [self.presentedViewController supportedInterfaceOrientations]; 
    } 
    else { 
     if (IS_IPAD()){ 
      return UIInterfaceOrientationMaskLandscapeRight; 
     } 
     else { 
      return UIInterfaceOrientationMaskAll; 
     } 
    } 
} 

// Determine iOS 6 Autorotation. 
- (BOOL)shouldAutorotate{ 
    UIDeviceOrientation orientation = [UIDevice currentDevice].orientation; 
    // Return yes to allow the device to load initially. 
    if (orientation == UIDeviceOrientationUnknown) return YES; 
    // Pass iOS 6 Request for orientation on to iOS 5 code. (backwards compatible) 
    BOOL result = [self shouldAutorotateToInterfaceOrientation:orientation]; 
    return result; 
} 
// handle iOS 5 Orientation as normal 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{ 
    // Return YES for supported orientations 

    if (self.presentedViewController != nil){ 
     return [self.presentedViewController shouldAutorotate]; 
    } 
    else { 
     if (IS_IPAD()){ 
      return (interfaceOrientation == UIInterfaceOrientationLandscapeRight); 
     } 
     else { 
      return (interfaceOrientation == UIInterfaceOrientationPortrait); 
     } 
    } 
} 
+0

感谢 - 但它仍然旋转。我得到这个代码错了吗? - (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientationation if(interfaceOrientation == UIInterfaceOrientationPortrait) { return YES; } else { return NO; } }' –

+0

对不起,我已将代码添加到问题中,因此更易于阅读。 –

+0

以及代码应该工作。 –

旋转的API已经在iOS6的改变。新的API显然被认为是可选的,但是它们似乎可以在模拟器或设备上启用所有的调试版本。要注册新API调用,请在您的APP Delegates didFinishLoading方法中插入类似的内容。

//Register for new API rotation calls 
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"UIApplicationSupportedInterfaceOrientationsIsEnabled"]; 

在旋转变化的心脏有两种方法(那里有第三,但进出口仍然搞清楚了这一点我自己)

- (BOOL)shouldAutorotate 
- (NSUInteger)supportedInterfaceOrientations 

你需要重写你的窗口RootViewController的这些方法。这意味着你需要继承UINavigationController或UITabBarController(如果任一个是你的根控制器的话)(这对我来说似乎很奇怪,但Apple说Jump)。

现在,如果所有你想要做的就是让你的应用程序纵向实现两种方法,你是金。

- (BOOL)shouldAutorotate 
{ 
    return NO; 
} 

- (NSUInteger)supportedInterfaceOrientations 
{ 
    return UIInterfaceOrientationMaskPortrait; 
} 

请注意,苹果通过添加界面方向遮罩,即进一步增加了混淆。 UIInterfaceOrientationMaskPortrait!= UIInterfaceOrientationPortrait。如果返回UIInterfaceOrientationPortrait,则行为将有所不同。此外,您可以将面具与面向结合的方式相结合,因此如果您想支持可以使用的两种纵向方向。

- (BOOL)shouldAutorotate 
{ 
    return YES; 
} 

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

这应该迫使纵向工作。现在,如果你愿意做一些事情,比如允许一个孩子控制器使用不同的方向,我不知道。

+0

你从哪里得到“UIApplicationSupportedInterfaceOrientationsIsEnabled”的信息?我在苹果网站上没有发现任何提及,也没有证据显示它有任何影响。 –

+0

我能找到它的唯一地方是在WWDC视频中讨论新的定位支持。我认为这是在[Session 200 - Cocoa Touch中的新功能](https://developer.apple.com/videos/wwdc/2012/?id=200)。 – tapi

在iOS6和iOS5中处理自动旋转的一种非常简单的方法是使用supportedInterfaceOrientations & shouldAutorotateToInterfaceOrientation。有一些宏使它成为一行代码。 UIInterfaceOrientationIsLandscape & UIInterfaceOrientationMaskLandscape。我发现UIInterfaceOrientationMaskLandscape & UIInterfaceOrientationMaskPortrait通过在xCode中自动完成。它不在Apple docs about autorotation

将此代码块添加到您的根视图控制器以强制它仅支持横向模式。

//iOS6 code to support orientations 
- (NSUInteger)supportedInterfaceOrientations 
{ 
    return (UIInterfaceOrientationMaskLandscape); 
} 

//iOS5 code to support orientations 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation 
{ 
    return (UIInterfaceOrientationIsLandscape(orientation)); 
} 

对于iOS6的,你可以使用以下方法来检测方向:

  • UIInterfaceOrientationMaskLandscape
  • UIInterfaceOrientationMaskPortrait
  • UIInterfaceOrientationMaskLandscapeRight
  • UIInterfaceOrientationMaskLandscapeLeft
  • UIInterfaceOrientationMaskPortraitUpsideDown
  • UIInterfaceOrientationMaskAllButUpsideDown
  • UIInterfaceOrientationMaskAll

对于iOS5的和下面你可以使用以下方法来检测方向:

  • UIInterfaceOrientationIsLandscape(宏)
  • UIInterfaceOrientationIsPortrait
  • UIInterfaceOrientationIsLandscapeRight
  • UIInterfaceOrientationIsLandscapeLeft
  • UIInterfaceOrientationIsPortraitUpsideDown