http://blog.sina.com.cn/s/blog_859ab3360101377j.html
在iOS6之前的版本中,通常使用
但是iOS 6里屏幕旋转改变了很多,之前的
- //
Applications should use supportedInterfaceOrient ations and/or shouldAutorotate.. - -
(BOOL)shouldAutorotateToInterf aceOrientation:(UIInterfaceOrientation)toInterfaceOrientation NS_DEPRECATED_IOS(2_0, 6_0);
程序将使用如下2个方法来代替:
- -
(BOOL)shouldAutorotate; - -
(NSUInteger)supportedInterfaceOrient ations;
1.
2.
另外要兼容IOS6之前的系统,要保留原来的
IOS6自动旋转设置:
IOS6里面,控制某个viewController旋转并不是像IOS5或者IOS4一样在这个viewController里面重写上面那2个方法,而是需要在这个viewController的rootViewController(根视图控制器)里面重写,怎么解释呢?就是最前面的那个viewController,直接跟self.window接触的那个controller,比如以下代码:
- UIViewController
*viewCtrl = [[UIViewController alloc] init]; - UINavigationController
*navCtrl = [[UINavigationController alloc] initWithRootViewControll er:viewCtrl]; - if
([window respondsToSelector:@selector(setRootViewController:)]) { -
self.window.rootViewController = navCtrl; - }
else { -
[self.window addSubview:navCtrl.view]; - }
- -(NSUInteger)supportedInterfaceOrient
ations{ -
return UIInterfaceOrientationMa skAllButUpsideDown; - }
-
- -
(BOOL)shouldAutorotate{ -
return YES; - }
eg1:如果上面的例子是self.window.rootViewController = viewCtrl,而不是navCtrl,那么上面的那2个控制旋转的方法就应该写在UIViewController里面!
eg2:如果viewCtrl又
pushViewController到viewCtrl2,需要设置viewCtrl2的旋转,怎么办呢? 还是在navCtrl里面控制,因为viewCtrl和viewCtrl2的rootViewController都是navCtrl,一般的写法都是
- UIViewController
*viewCtrl2 = [[UIVewController alloc] init]; - [self.navigationController.navigationController
pushViewController:viewCtrl2 animated:YES];
所以要控制一个UINavigationController push到的所有viewController的旋转,那么就得在navCtrl里面区分是哪个viewController,以便对他们一一控制!同样如果rootViewController是UITabbarController,那么需要在子类化的
UITabbarController里面重写那2个方法,然后分别控制!
但是有时候我初始化UINavigationController的时候,并不知道所有我所有需要push到的viewController,那么这里有一个通用的方法,就是让viewController自己来控制自己,首先在navCtrl里面的实现方法改为以下方式:
- -
(BOOL)shouldAutorotate - {
-
return self.topViewController.shouldAutorotate; -
//topViewController为UINavigationController的属性 - }
-
- -
(NSUInteger)supportedInterfaceOrient ations - {
-
return self.topViewController.supportedInterfaceOrient ations; - }
eg3:如果viewCtrl 是
这个简单很多,没有上面的硬性条件,只需要在需要旋转的viewController里面重写 shouldAutorotateToInterf aceOrientation 方法就行
手动旋转
手动旋转也有2种方式,一种是直接设置
- if
([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) { -
[[UIDevice currentDevice] performSelector:@selector(setOrientation:) withObject:(id)UIInterfaceOrientationPo rtrait]; - }
- self.view.transform
= CGAffineTransformMakeRot ation(M_PI/2)
下面讲解采用第二种方式的各版本手动旋转:
思想是首先设置
IOS6手动旋转:
1. 那既然是旋转,最少也得有2个方向,那么还是少不了上面说的那个硬性条件,先在plist里面设置好所有可能需要旋转的方向。既然是手动旋转,那么就要关闭自动旋转:
- -
(BOOL)shouldAutorotate{ -
return NO; - }
- [[UIApplication
sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLa ndscapeRight]; - self.view.transform
= CGAffineTransformMakeRot ation(M_PI/2); - self.view.bounds
= CGRectMake(0, 0, kScreenHeight, 320);
1. 只需要改变self.view.transform,那么self.view的所有subview都会跟着自动变;其次因为方向变了,所以self.view的大小需要重新设置,不要使用self.view.frame,而是用bounds。
2. 如果shouldAutorotate 返回YES的话,下面设置setStatusBarOrientation 是不管用的!setStatusBarOrientation只有在shouldAutorotate 返回NO的情况下才管用!
IOS5、IOS4手动旋转:
1.在需要手动旋转的viewController里的 shouldAutorotateToInterf- -
(BOOL)shouldAutorotateToInterf aceOrientation:(UIInterfaceOrientation)interfaceOrientation{ -
return (interfaceOrientation == [UIApplication sharedApplication].statusBarOrientation); - }
- [[UIApplication
sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLa ndscapeRight]; - self.view.transform
= CGAffineTransformMakeRot ation(M_PI/2); - self.view.bounds
= CGRectMake(0, 0, kScreenHeight, 320);
经验分享:
1.IOS6里面,如果一个项目里面需要各种旋转支持,有自动,有手动,那么我们可以新建2个navController或者tabbarController的子类,一个是不旋转,一个旋转,那么所有需要旋转的UINavigationController都可以用这个子类来代替!包括我们可以定制短信呀、邮件呀的旋转!2.supportedInterfaceOrient
- -(NSUInteger)supportedInterfaceOrient
ations{ -
return [UIApplication sharedApplication].statusBarOrientation; - }
- UIInterfaceOrientationLa
ndscapeLeft = UIDeviceOrientationLands capeRight, - UIInterfaceOrientationLa
ndscapeRight = UIDeviceOrientationLands capeLeft
参考:
http://blog.youkuaiyun.com/totogogo/article/details/8002173
http://stackoverflow.com/questions/13200220/how-to-change-keyboard-orientation-in-ios6
http://blog.youkuaiyun.com/yiyaaixuexi/article/details/8035014