概述:
在iOS6之前的版本中,通常使用 shouldAutorotateToInterfaceOrientation 来单独控制某个UIViewController的方向,需要哪个viewController支持旋转,只需要重写shouldAutorotateToInterfaceOrientation方法。
但是iOS 6里屏幕旋转改变了很多,之前的 shouldAutorotateToInterfaceOrientation 被列为 DEPRECATED 方法,查看UIViewController.h文件也可以看到:
- // Applications should use supportedInterfaceOrientations and/or shouldAutorotate..
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation NS_DEPRECATED_IOS(2_0, 6_0);
程序将使用如下2个方法来代替:
- - (BOOL)shouldAutorotate;
- - (NSUInteger)supportedInterfaceOrientations;
除了重写这个2个方法,IOS6里面要旋转还有一些需要注意的地方,下面会细述。另外还有一个硬性条件,需要在Info.plist文件里面添加程序支持的所有方向,可以通过以下2种方式添加
1.
2.
另外要兼容IOS6之前的系统,要保留原来的 shouldAutorotateToInterfaceOrientation 方法,还有那些 willRotateToInterfaceOrientation 等方法。
IOS6自动旋转设置:
- UIViewController *viewCtrl = [[UIViewController alloc] init];
- UINavigationController *navCtrl = [[UINavigationController alloc] initWithRootViewController:viewCtrl];
- if ([window respondsToSelector:@selector(setRootViewController:)]) {
- self.window.rootViewController = navCtrl;
- } else {
- [self.window addSubview:navCtrl.view];
- }
- -(NSUInteger)supportedInterfaceOrientations{
- return UIInterfaceOrientationMaskAllButUpsideDown;
- }
- - (BOOL)shouldAutorotate{
- return YES;
- }
- UIViewController *viewCtrl2 = [[UIVewController alloc] init];
- [self.navigationController.navigationController pushViewController:viewCtrl2 animated:YES];
- - (BOOL)shouldAutorotate
- {
- return self.topViewController.shouldAutorotate;
- }
- - (NSUInteger)supportedInterfaceOrientations
- {
- return self.topViewController.supportedInterfaceOrientations;
- }
eg3:如果viewCtrl 是 presentModalViewController 到 viewCtrl3,那么viewCtrl3的旋转设置就不在navCtrl里面了!如果presentModalViewController的viewController是navController、tabbarController包装过的viewCtrl3,那么就应在新包装的navController、tabbarController里面设置,如果是直接presentModalViewController到viewCtrl3,那么就在viewCtrl3里面设置
手动旋转
手动旋转也有2种方式,一种是直接设置 UIDevice 的 orientation,但是这种方式不推荐,上传appStore有被拒的风险:
- if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
- [[UIDevice currentDevice] performSelector:@selector(setOrientation:) withObject:(id)UIInterfaceOrientationPortrait];
- }
- self.view.transform = CGAffineTransformMakeRotation(M_PI/2)
下面讲解采用第二种方式的各版本手动旋转:
思想是首先设置 statusBarOrientation,然后再改变某个view的方向跟 statusBarOrientation 一致!
IOS6手动旋转:
1. 那既然是旋转,最少也得有2个方向,那么还是少不了上面说的那个硬性条件,先在plist里面设置好所有可能需要旋转的方向。既然是手动旋转,那么就要关闭自动旋转:
- - (BOOL)shouldAutorotate{
- return NO;
- }
- [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
- self.view.transform = CGAffineTransformMakeRotation(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的情况下才管用!
http://blog.csdn.net/qq515383106/article/details/8765360