iOS 6中的游戏中心登录仅支持横屏锁定。

20

游戏中心加载时的默认方向为竖屏。为了将其锁定在横屏模式下,添加了一个类别。

@implementation GKMatchmakerViewController (LandscapeOnly)

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return ( interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}

- (BOOL)shouldAutorotate {
    return NO;
}
@end

在iOS 6以下版本中运行良好,但在iOS 6上会显示错误。

终止应用程序,原因是未捕获的异常'UIApplicationInvalidInterfaceOrientation':'支持的方向与应用程序没有共同的方向,并且shouldAutorotate返回YES'

请解释一种解决方案。

2个回答

39

最终,我通过遵循苹果iOS 6发行说明中提到的解决方法成功避免了崩溃。

解决方法:

1.应用程序应提供委托方法application:supportedIntefaceOrientationsForWindow,并确保纵向是返回掩码值之一。

- (NSUInteger)application:(UIApplication*)application supportedInterfaceOrientationsForWindow:(UIWindow*)window
{

    return UIInterfaceOrientationMaskAllButUpsideDown;
}

2. 当涉及到UIBNavigationController(或者UIViewController)时,可以通过子类化UINavigationController/UIViewController并重写supportedInterfaceOrientations来实现。

 - (NSUInteger)supportedInterfaceOrientations
    {
        return UIInterfaceOrientationMaskLandscape;
    }

同时,

在构建摘要中支持选择横向左右和横向右。

现在游戏中心可以正常工作而不会崩溃。


谢谢!我的屁股也被救了 :) - Oren Bengigi
1
对我也起作用了,但在我的情况下,我没有使用UIBNavigationController而是使用UIViewController(它的子类),然而我仍然不得不将方法2添加进去。你可能想要在这个答案中将UIBNavigationController替换为UIViewController。 - Steph Thirion
我添加了那个信息。你的答案现在比苹果的发布说明更准确。 ;) - Steph Thirion
1
它能够工作,但我的应用现在支持竖屏,我该如何阻止它进入竖屏模式。请帮忙 :( - Arnlee Vizcayno
只是一个小注释:在Appdelegate中附加到NavigationController类的一个类别更加优雅,而不是创建一个新的子类,但无论如何这都是一个可行的解决方案。 - BootMaker

0

还需要添加一点东西。为了解决这个愚蠢的问题,已经纠结了两天。如果上述方法没有帮助,并且你使用了UINavigationController(并且已经对其进行了子类化),你需要在appDelegate中添加以下代码:

[window setRootViewController:navigationController]; // use this
// instead of [self.window addSubview:navigationController.view];

谢谢http://grembe.wordpress.com/2012/09/19/here-is-what-i/


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接