iOS 屏幕旋转问题
2015-10-26 14:51
393 查看
http://blog.csdn.net/wudizhukk/article/details/8674393
self.interfaceOrientation或[[UIApplication sharedApplication] statusBarOrientation]
if (self.interfaceOrientation==UIDeviceOrientationLandscapeRight) {
XXOO
}
不旋转,保持竖屏
//iOS 5
- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}
//iOS 6
- (BOOL)shouldAutorotate
{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
始终保持横屏
//iOS 5
- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return (toInterfaceOrientation == self.preferredInterfaceOrientationForPresentation);
}
//iOS 6
- (BOOL) shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscapeRight;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeRight;
}
在使用UINavigationController时发现,无论怎么设置上面方法的返回,都无法控制UINavigationController的旋转,这似乎是iOS的一个bug。但无论如何,以下是stackflow上面的一个解决方法:
1 @implementation UINavigationController (Rotation_IOS6)
2 -(BOOL)shouldAutorotate {
3 return [[self.viewControllers lastObject] shouldAutorotate];
4 }
5
6 -(NSUInteger)supportedInterfaceOrientations {
7 return [[self.viewControllers lastObject] supportedInterfaceOrientations];
8 }
9
10 - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
11 return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
12 }
13 @end
屏幕旋转方法调用流程
要翻转的时候,首先响应的方法:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
return YES则支持翻转,NO则不支持。
紧接着
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
被调用。这个方法是发生在翻转开始之前。一般用来禁用某些控件或者停止某些正在进行的活动,比如停止视频播放。
再来是
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
这个方法发生在翻转的过程中,一般用来定制翻转后各个控件的位置、大小等。可以用另外两个方法来代替:willAnimateFirstHalfOfRotationToInterfaceOrientation:duration: 和 willAnimateSecondHalfOfRotationFromInterfaceOrientation:duration:
最后调用的是
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
这个方法发生在整个翻转完成之后。一般用来重新启用某些控件或者继续翻转之前被暂停的活动,比如继续视频播放
self.interfaceOrientation或[[UIApplication sharedApplication] statusBarOrientation]
if (self.interfaceOrientation==UIDeviceOrientationLandscapeRight) {
XXOO
}
不旋转,保持竖屏
//iOS 5
- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}
//iOS 6
- (BOOL)shouldAutorotate
{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
始终保持横屏
//iOS 5
- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return (toInterfaceOrientation == self.preferredInterfaceOrientationForPresentation);
}
//iOS 6
- (BOOL) shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscapeRight;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeRight;
}
在使用UINavigationController时发现,无论怎么设置上面方法的返回,都无法控制UINavigationController的旋转,这似乎是iOS的一个bug。但无论如何,以下是stackflow上面的一个解决方法:
1 @implementation UINavigationController (Rotation_IOS6)
2 -(BOOL)shouldAutorotate {
3 return [[self.viewControllers lastObject] shouldAutorotate];
4 }
5
6 -(NSUInteger)supportedInterfaceOrientations {
7 return [[self.viewControllers lastObject] supportedInterfaceOrientations];
8 }
9
10 - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
11 return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
12 }
13 @end
屏幕旋转方法调用流程
要翻转的时候,首先响应的方法:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
return YES则支持翻转,NO则不支持。
紧接着
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
被调用。这个方法是发生在翻转开始之前。一般用来禁用某些控件或者停止某些正在进行的活动,比如停止视频播放。
再来是
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
这个方法发生在翻转的过程中,一般用来定制翻转后各个控件的位置、大小等。可以用另外两个方法来代替:willAnimateFirstHalfOfRotationToInterfaceOrientation:duration: 和 willAnimateSecondHalfOfRotationFromInterfaceOrientation:duration:
最后调用的是
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
这个方法发生在整个翻转完成之后。一般用来重新启用某些控件或者继续翻转之前被暂停的活动,比如继续视频播放
相关文章推荐
- iOS首页广告滚动栏循环
- IOS--JSON介绍
- IOS渐变图层CAGradientLayer
- IOS中的XML解析之DOM和SAX
- iOS GCD中的dispatch_group
- ios 类似淘宝评论星星功能
- iOS - 应用程序国际化
- iOS开发 - NSBundle, NSDevice, NSLocale
- class-dump 使用
- ios如何获取xib自动布局后视图的宽度和高度
- IOS--─自定义加载等待框(MBProgressHUD)
- IOS 然你的新建的view 盖住系统键盘的做法
- IOS--错误集锦--Undefined symbols for architecture x86_64:
- iOS 关于本地持久化存储的探讨
- 关于升级iOS9.0之后,项目中若干错误的解决方法
- IOS 第三方库 - SDWebImage
- IOS 开发,调用打电话,发短信,打开网址
- 工具推荐 - windows下iOS设备越狱的
- iOS开发——keychain的使用
- iOS - 打电话, 发短信