为什么IOS6屏幕无法旋转?
来源:互联网 发布:淘宝crm系统架构 编辑:程序博客网 时间:2024/05/07 02:59
这篇为原创加转载。。。。
在网上看了很多关于IOS6屏幕旋转的帖子,说的基本都一样,主要在于两个方法的调用和一个地方的设置:
两个方法:
-(NSUInteger)supportedInterfaceOrientations{
NSLog(@"exe the supportedInterfaceOrientations.......");
return UIInterfaceOrientationMaskLandscape;
}
- (BOOL)shouldAutorotate{
NSLog(@"exe the shouldAutorotate.......");
return YES;
}
一个地方:
这两个方法如何调用和这个地方如何设置请看后面转载,这里重点要说的是,为什么有些同学(包括我)按照这个步骤设置之后,
屏幕还是无法旋转,为什么了呢?
问题在于这里(反正我是这样的):
看这里:
[self.window addSubview:navCtrl.view];
与
self.window.rootViewController = navCtrl;
的却别!
没有旋转的同学你们用的应该是[self.window addSubview:navCtrl.view];这句代码。
你只需将它改为self.window.rootViewController = navCtrl;即可。
原因在哪里,在于view与rootViewController的却别,我就不解释了,自己理解去吧
/*************************************************************************************************************/
下面内容转自:http://blog.csdn.net/cococoolwhj/article/details/8208991
概述:
在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;
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里面设置
IOS5、IOS4自动旋转设置
手动旋转
手动旋转也有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的情况下才管用!
IOS5、IOS4手动旋转:
1.在需要手动旋转的viewController里的 shouldAutorotateToInterfaceOrientation 方法设置 interfaceOrientation == [UIApplicationsharedApplication].statusBarOrientation- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
- return (interfaceOrientation == [UIApplication sharedApplication].statusBarOrientation);
- }
- [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
- self.view.transform = CGAffineTransformMakeRotation(M_PI/2);
- self.view.bounds = CGRectMake(0, 0, kScreenHeight, 320);
经验分享:
1.IOS6里面,如果一个项目里面需要各种旋转支持,有自动,有手动,那么我们可以新建2个navController或者tabbarController的子类,一个是不旋转,一个旋转,那么所有需要旋转的UINavigationController都可以用这个子类来代替!包括我们可以定制短信呀、邮件呀的旋转!2.supportedInterfaceOrientations 方法一般是写UIInterfaceOrientationMask方向,但是如果程序要兼容4.3以下的SDK(4.3以下的SDK必须是4.5以下的Xcode,不支持IOS6),那么在用4.5以下的Xcode编译的时候通不过!所以可以用statusBarOrientation代替或者直接写死数字!
- -(NSUInteger)supportedInterfaceOrientations{
- return [UIApplication sharedApplication].statusBarOrientation;
- }
- UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight,
- UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeLeft
参考:
http://blog.csdn.net/totogogo/article/details/8002173
http://stackoverflow.com/questions/13200220/how-to-change-keyboard-orientation-in-ios6
http://blog.csdn.net/yiyaaixuexi/article/details/8035014
- 为什么IOS6屏幕无法旋转?
- iOS6屏幕旋转
- iOS6屏幕旋转
- IOS6屏幕旋转
- ios6 屏幕旋转总结
- iOS6屏幕旋转
- IOS6屏幕旋转问题
- 关于IOS6屏幕旋转
- ios6 屏幕旋转问题
- ios6 屏幕旋转
- IOS6屏幕旋转详解
- IOS6屏幕旋转详解
- ios6 屏幕旋转总结
- IOS6屏幕旋转问题总结 .
- iOS6以上屏幕旋转详解
- iOS6下关于屏幕旋转的控制
- 关于iOS6.0 屏幕旋转的问题
- UI -- IOS6屏幕旋转问题总结
- mysql数据去除重复记录小结
- 使用ajax得到返回值时执行顺序问题
- Install TestLink on Linux Server
- Ogre基础教程(一)----- 场景管理器,场景节点和实体.
- android 将时间戳转为代表"距现在多久之前"的字符串
- 为什么IOS6屏幕无法旋转?
- 惯性导航
- Android中如何解决输入法键盘和activity页面遮挡的问题
- Linux 内核启动分析
- RMAN恢复案例之三
- 写一个实现用户注册时,向其邮箱发送激活码邮件,并进行状态处理。
- List All Of The Subset In Another Method
- s5pv210 中配置gpio为中断引脚。
- Linux下使用Tuxedo的例子