【转载】iOS6中gamecenter限制横竖屏方法及其他窗口相关细节

  • 本帖最后由 老G 于 2012-10-11 11:22 编辑 *

iOS6一出,立马天下大乱,各种应用分辨率横竖屏统统乱套,SDK引擎纷纷升级。我们一边诅咒苹果坑爹的同时,一边到处寻找解决方案。转一个写得不错的文章,供参考。

原帖地址:http://blog.k-res.net/设计开发/ios6对于shouldautorotatetointerfaceorientation的改动以及其他一些窗口相关细节.html

http://blog.k-res.net/设计开发/ios6对于shouldautorotatetointerfaceorientation的改动以及其他一些窗口相关细节.html
九月 26, 2012 | Posted by K-Res
http://blog.k-res.net/tag/ios6正式版发布当天博主我就更新了,随后也更新了对应的XCode以及http://blog.k-res.net/tag/ios SDK,更新到了4.5 (4G182)。然后更新原有4.4 http://blog.k-res.net/tag/ios5 SDK的项目,目前最主要的发现就是http://blog.k-res.net/tag/ios6对于app屏幕朝向支持以及自动旋屏时的处理方式的变动。简而言之就是http://blog.k-res.net/tag/ios6下的
1- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation

2{

3 return UIInterfaceOrientationIsLandscape(toInterfaceOrientation);

4}

这个不会再被调用,取而代之的是这俩个组合:
1- (BOOL)shouldAutorotate

2{

3 return YES;

4}

5

6

  • (NSUInteger)supportedInterfaceOrientations

7{

8 return UIInterfaceOrientationMaskLandscape;

9}

当然,为了保持对旧版本系统行为的兼容性,不要删掉不用的那个调用。另外还有一个这个preferred朝向也可以加上
1- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation

2{

3 return UIInterfaceOrientationLandscapeRight;

4}

当我替换完这俩个操作后尝试运行app,发现会报如下的异常:
Terminating app due to uncaught exception ‘UIApplicationInvalidInterfacehttp://blog.k-res.net/tag/orientation’, reason: ‘Supported orientations has no common orientation with the application, and shouldAutorotate is returning YES’

经查发现导致此异常的原因是app再info.plist中指定的屏幕朝向没有portrait,也就是只支持landscape横屏,但是app集成了Game Center应用,而Game Center触发的登录界面只支持竖屏显示(这点有开发帐号的朋友可以到苹果官方开发论坛上看下,有个苹果官方人员发的件实贴,由于现阶段的NDA就不转了),解决这个问题的方法就是再应用的delegate中加入如下回调:
1- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window

2{

3 if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)

4 return UIInterfaceOrientationMaskAll;

5 else /* iphone */

6 return UIInterfaceOrientationMaskAllButUpsideDown;

7}

这样就可以再不改变info.plist中的设置的前提下,兼容gamecenter的竖屏登录问题。
顺带一提,对于Game Center排行等界面的旋屏朝向限制方法:
1@implementation GKLeaderboardViewController(Landscape)

2

3

  • (BOOL)shouldAutorotate

4{

5 return YES;

6}

7

8

  • (NSUInteger)supportedInterfaceOrientations

9{

10 returnUIInterfaceOrientationMaskLandscapeRight|UIInterfaceOrientationMaskLandscapeLeft;

11}

12

13

  • (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation

14{

15 return UIInterfaceOrientationLandscapeRight;

16}

17

18

  • (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

19{

20 return UIInterfaceOrientationIsLandscape(interfaceOrientation);

21}

22

23
@end

这个时候确实感到objective-c的category之方便啊,呵呵。再有一个问题就是window的rootViewController问题,在http://blog.k-res.net/tag/ios6下,必须通过setRootViewController指定根视图控制器,否则didFinishLaunchingWithOptions结束后会报必须指定根视图控制器的错误。网上看到有人提到在iOS6下addSubview的方法将不再起作用,而必须使用指定rootViewController的方式,而我实际测试时还发现了另外一个小问题,就是在切换view的时候,比如用MPMoviePlayerViewController播放intro视频,然后再切换到游戏OpenGL视图时的addSubView和removeFromSuperview的交替会出现一些奇怪的GL视图朝向错误问题,而且也找不到任何相关旋屏的log输出,最后发现只要是用了iOS6的SDK就必须调用setRootViewController,iOS6以前的系统还需要额外调用addSubview,这样就不会出现那种切换view后的诡异问题了,至少我目前找到的解决方法是这样,不知是否另有玄机?附上改后的根据系统版本号执行不同切换方式的简单代码:
1if (.systemVersion floatValue]<6.0)

2{

3 // 高兼容性的做法

4 ;

5 ;

6}

7else

8{

9 ;

10}

11;

最后再爆料一个屏幕初始朝向的问题,我们知道,设定屏幕初始朝向的方法是再info.plist中指定Initial interface orientation项,而我发现无论怎么设置,横屏都只能以landscape-left的方式启动,就是按钮在左边那种,不管是升级的旧项目,还是新建的项目均是如此,不知是否有朋友也遇到了这个问题?