转自:http://iliao.diandian.com/post/2013-02-21/40049597269
1.将addSubview修改为setRootViewController
[window addSubview: viewController.view];修改如下:
if ( [[UIDevice currentDevice].systemVersion floatValue] < 6.0)
{
// warning: addSubView doesn't work on iOS6
[window addSubview: viewController.view];
}
else
{
// use this mehod on ios6
[window setRootViewController:viewController];
}
2.修改shouldAutorotateToInterfaceOrientation
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return UIInterfaceOrientationIsLandscape( interfaceOrientation );
}
iOS6以前会呼叫这方法来判断手机是垂直或打横
// For ios6, use supportedInterfaceOrientations & shouldAutorotate instead of shouldAutorotateToInterfaceOrientation
- (NSUInteger) supportedInterfaceOrientations{
return UIInterfaceOrientationMaskLandscape;
}
- (BOOL) shouldAutorotate {
return YES;
}
iOS6需在加上上面方法
在iOS5.1 和 之前的版本中, 我们通常利用 shouldAutorotateToInterfaceOrientation: 来单独控制某个UIViewController的旋屏方向支持,也就是说各个ViewController各自独立管理自己的旋转方向,跟其他ViewController或NavigationController没有关系,比如:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{//interfaceOrientation表示当前设备旋转到的方向,返回YES则表示界面也旋转到这个方向,NO则不
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
但是在iOS6中,这个方法被废弃了,使用无效
在IOS6中的每个ViewController中代替使用:
-(BOOL)shouldAutorotate
{
return NO;//YES 支持旋转到supportedInterfaceOrientations函数所指定的方向 NO 则完全不支持旋转 始终保持正屏(UIInterfaceOrientationMaskPortrait)
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait; //当shouldAutorotate函数返回YES 指定的值才起作用
}
这里有一个问题,当是用UINavigationController管理ViewController时,在各个viewController中制定的这两个函数必须要一样,不然会崩,于是一般自定义UINavigationController,然后在这里面重写这两个方法,这样之后,在每个具体ViewController中的指定就不会起作用,这样便于统一各个界面的旋转风格,但不适合有界面旋转风格不同的情况
for ios 4 and 5, 如果没有重写shouldAutorotateToInterfaceOrientation,那么对于iphone来讲,by default是只支持portrait,不能旋转。
for ios 6, 如果没有重写shouldAutorotate and supportedInterfaceOrientations,by default, iphone则是"可以旋转,支持非upside down的方向",而ipad是"可以选择,支持所有方向"