由于设备升级到了ios6的系统,所以为了调试,xcode也升级到了4.5,但是,当我运行以前的cocos2d工程的时候,发现本来是横屏的游戏,变成了竖屏,而且新建的cocos2d工程也是有这个问题。
所以经过一系列的调试,修改,查阅文档。发现最新的帮助文档里面写到:
In iOS 6, your app supports the interface orientations defined in your app’s Info.plist
file. A view controller can override the supportedInterfaceOrientations
method to limit the list of supported orientations. Generally, the system calls this method only on the root view controller of the window or a view controller presented to fill the entire screen。
上面红色着重标记了一下关键字。大概意思也就是在ios6中,一般是在Info.plist里面来设置这个app需要的方向,或者重写
supportedInterfaceOrientations这个方法,而且为了系统能够调用这些东西,你需要设置这个view controller是这个window的root view controller或者弹出充满整个屏幕这个一个view controller。而在coocos2d模板创建的工程当中使用的是:
[window addSubview: viewController.view];
所以我们怎么设置Info.plist都是没有用的。而且view controller官方文档直接写明了:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation NS_DEPRECATED_IOS(2_0, 6_0);
这个shouldAutorotateToInterfaceOrientation在ios6中被弃用了,所以在RootViewController.m中这个函数怎么都不会被调用了。
不过在这个类里面还有一个
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
不过经过测试也发现没有被调用。
这个时候,你要是把addsubview那句改成:
[window setRootViewController:viewController];
这个时候,你就可以发现你的cocos2d工程恢复了正常了,而且willRotateToInterfaceOrientation也被调用了。