【问题标题】:Can't auto rotate in iOS 6无法在 iOS 6 中自动旋转
【发布时间】:2013-04-08 05:40:30
【问题描述】:

在我维护的应用程序中,应该在纵向和纵向倒置模式下发生旋转。 (所有旋转都在摘要面板中启用。)

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return interfaceOrientation==UIInterfaceOrientationPortrait || interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown;
}

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

无论我尝试什么,我都无法让旋转以准确地运行 i ios 6

到目前为止我尝试过的事情:

-(NSUInteger)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskPortrait; 
}

-(NSInteger)supportedInterfaceOrientations{
NSInteger mask = 0;
if ([self shouldAutorotateToInterfaceOrientation: UIInterfaceOrientationLandscapeRight])
    mask |= UIInterfaceOrientationMaskLandscapeRight;
if ([self shouldAutorotateToInterfaceOrientation: UIInterfaceOrientationLandscapeLeft])
    mask |= UIInterfaceOrientationMaskLandscapeLeft;
if ([self shouldAutorotateToInterfaceOrientation: UIInterfaceOrientationPortrait])
    mask |= UIInterfaceOrientationMaskPortrait;
if ([self shouldAutorotateToInterfaceOrientation: UIInterfaceOrientationPortraitUpsideDown])
    mask |= UIInterfaceOrientationMaskPortraitUpsideDown;
return mask;
}

我试着把它放在我的 appdelegate 中:

-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    return UIInterfaceOrientationMaskPortraitUpsideDown;
}

但我收到此错误:由于未捕获的异常“UIApplicationInvalidInterfaceOrientation”而终止应用程序,原因:“支持的方向与应用程序没有共同的方向,并且 shouldAutorotate 正在返回 YES”

尝试将其放入我的委托中:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    NSUInteger orientations = UIInterfaceOrientationMaskAll;

    if (self.window.rootViewController) {
        UIViewController* presented = [[(UINavigationController *)self.window.rootViewController viewControllers] lastObject];
        orientations = [presented supportedInterfaceOrientations];
    }
    return orientations; 
}

我阅读了所有关于它的讨论以及对 shouldAutorotateToInterfaceOrientation 的弃用,但我仍然无法让它发挥作用。

我即将失去它

【问题讨论】:

  • 尝试创建一个类别并处理方向。它对我来说工作正常。
  • 你能提供一种方法\一些代码吗?
  • 删除你的方法并添加代码并让我知道 -(BOOL)shouldAutorotate { return YES; } -(NSInteger)supportedInterfaceOrientations{ 返回 UIInterfaceOrientationMaskAll; }
  • 并将应用程序委托方法更改为此 -(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { return UIInterfaceOrientationMaskAll; }
  • 我仍然无法让它工作。

标签: objective-c cocoa-touch cocoa ios6


【解决方案1】:

来自 Apple 的 iOS 6 SDK 发行说明:

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

更多的责任正在转移到应用和应用代理身上。现在,iOS 容器(例如UINavigationController)不会咨询他们的孩子来确定他们是否应该自动旋转。默认情况下,应用程序和视图控制器支持的界面方向设置为 iPad 惯用语的 UIInterfaceOrientationMaskAll 和 iPhone 惯用语的 UIInterfaceOrientationMaskAllButUpsideDown

视图控制器支持的界面方向会随着时间而改变——即使是应用支持的界面方向也会随着时间而改变。每当设备旋转或视图控制器以全屏模式呈现样式呈现时,系统都会向最顶层的全屏视图控制器(通常是根视图控制器)询问其支持的界面方向。此外,仅当此视图控制器从其 shouldAutorotate 方法返回 YES 时,才会检索支持的方向。系统将视图控制器支持的方向与应用支持的方向(由 Info.plist 文件或应用代理的application:supportedInterfaceOrientationsForWindow: 方法确定)相交以确定是否旋转。

系统通过将应用程序的supportedInterfaceOrientationsForWindow: 方法返回的值与最顶部全屏控制器的supportedInterfaceOrientations 方法返回的值相交来确定是否支持方向。 setStatusBarOrientation:animated: 方法并未完全弃用。它现在仅在最顶层全屏视图控制器的 supportedInterfaceOrientations 方法返回 0 时才有效。这使得调用者负责确保状态栏方向一致。

为了兼容性,仍然实现shouldAutorotateToInterfaceOrientation: 方法的视图控制器不会获得新的自动旋转行为。 (换句话说,他们不会回退到使用应用程序、应用程序委托或 Info.plist 文件来确定支持的方向。)相反,shouldAutorotateToInterfaceOrientation: 方法用于合成将由 @ 返回的信息987654334@方法。

如果您希望整个应用旋转,那么您应该将 Info.plist 设置为支持所有方向。现在,如果您希望特定视图仅是纵向的,则必须执行某种子类并覆盖自动旋转方法以仅返回纵向。看看How to force a UIViewController to Portrait orientation in iOS 6

【讨论】:

  • 我的目标摘要设置为支持所有方向,因为我的 Info.plist 设置为支持所有方向。
  • 我将链接中的方法添加到我的 UINavigationController 子类中,我可以看到这些函数正在被调用,但即使我翻转设备,屏幕也会保持纵向模式
  • 哦..发生了。稍等,我会修改我的答案。
【解决方案2】:

我的应用程序以 IOS 5 为目标。我对 IOS 5 设备使用了 shouldAutorotateToInterfaceOrientation 方法(默认情况下)。并分类 UINavigationController 来处理 IOS 6 的方向。

#import "UINavigationController+Rotation_IOS6.h"

    @implementation UINavigationController (Rotation_IOS6)

    -(BOOL)shouldAutorotate
    {

            return YES;
    }

    -(NSUInteger)supportedInterfaceOrientations
    {

        return UIInterfaceOrientationMaskAll;
    }

    - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
    {
        if([self.visibleViewController isMemberOfClass:NSClassFromString(@"SampleViewController")])
        {
            return UIInterfaceOrientationMaskLandscape | UIInterfaceOrientationMaskPortraitUpsideDown | UIInterfaceOrientationMaskPortrait;
        }
        return UIInterfaceOrientationPortrait;

    }

    @end

【讨论】:

  • 我将上面的内容添加到我的 UINavigationController 子类中,我可以看到 supportedInterfaceOrientations 正在被调用,但方向仍然不会改变。
【解决方案3】:

检查您的目标属性...如下所示

【讨论】:

    【解决方案4】:
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
        return YES;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-08
      • 2012-09-15
      • 1970-01-01
      • 1970-01-01
      • 2012-09-13
      相关资源
      最近更新 更多