iOS强制横屏
2015-08-18 11:33
627 查看
在做视频播放时需要视频播放页面强制横屏,其他页面依然只支持竖屏,下面是使用过的两种方式。
iOS强制横屏的两种方式:
第1种:设置状态栏方向,然后vc.view设置transform旋转。注意:VC需要设置为只支持竖屏。
然后重写VC屏幕旋转方法为只支持竖向:
第2种:强制横屏的VC需支持屏幕旋转,但只支持横向,其他vc都需要设置自己对屏幕方向的支持。
强制横屏VC最好使用自动布局,或者重写viewWillLayoutSubviews和viewDidLayoutSubviews,实现进行页面布局。
需要强制横屏的VC的屏幕方向支持代码:
其他不需要支持横屏的VC的代码(建议放在BaseVC):
然后也是最关键的地方如果使用navgationController请使用其子类,并在子类种重写屏幕旋转方法:
================================
做视频播放时需强制横屏,开始使用第一种方式实现,后因AirPlay选择框显示依然是竖屏方向,改用了第二种方式,如果可以最好使用第二种方式。
iOS强制横屏的两种方式:
第1种:设置状态栏方向,然后vc.view设置transform旋转。注意:VC需要设置为只支持竖屏。
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:YES]; [UIView animateWithDuration:0.25 animations:^{ self.view.transform = CGAffineTransformMakeRotation(M_PI/2); self.view.bounds = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.height, [[UIScreen mainScreen] bounds].size.width); } completion:^(BOOL finished) { }];
然后重写VC屏幕旋转方法为只支持竖向:
#pragma mark //屏幕旋转 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { return (toInterfaceOrientation == UIInterfaceOrientationPortrait); } - (BOOL)shouldAutorotate { return NO; } - (NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait;//只支持这一个方向(正常的方向) }
第2种:强制横屏的VC需支持屏幕旋转,但只支持横向,其他vc都需要设置自己对屏幕方向的支持。
强制横屏VC最好使用自动布局,或者重写viewWillLayoutSubviews和viewDidLayoutSubviews,实现进行页面布局。
需要强制横屏的VC的屏幕方向支持代码:
#pragma mark 屏幕旋转 //iOS5 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { return (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft||toInterfaceOrientation == UIInterfaceOrientationLandscapeRight); } //iOS6+ - (BOOL)shouldAutorotate { return YES; } - (NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskLandscape; }
其他不需要支持横屏的VC的代码(建议放在BaseVC):
#pragma mark //屏幕旋转 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { return (toInterfaceOrientation == UIInterfaceOrientationPortrait); } - (BOOL)shouldAutorotate { return NO; } - (NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait;//只支持这一个方向(正常的方向) }
然后也是最关键的地方如果使用navgationController请使用其子类,并在子类种重写屏幕旋转方法:
#pragma mark //屏幕旋转 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { if ([self.topViewController respondsToSelector:@selector(shouldAutorotateToInterfaceOrientation:)]) { return [self.topViewController shouldAutorotateToInterfaceOrientation:toInterfaceOrientation]; } return (toInterfaceOrientation == UIInterfaceOrientationPortrait); } - (BOOL)shouldAutorotate { if ([self.topViewController respondsToSelector:@selector(shouldAutorotate)]) { return [self.topViewController shouldAutorotate]; } return NO; } - (NSUInteger)supportedInterfaceOrientations { if ([self.topViewController respondsToSelector:@selector(supportedInterfaceOrientations)]) { return [self.topViewController supportedInterfaceOrientations]; } return UIInterfaceOrientationMaskPortrait;//只支持这一个方向(正常的方向) }
================================
做视频播放时需强制横屏,开始使用第一种方式实现,后因AirPlay选择框显示依然是竖屏方向,改用了第二种方式,如果可以最好使用第二种方式。
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- Android如何禁止横屏竖屏的变换
- js判断客户端是iOS还是Android等移动终端的方法
- IOS开发环境windows化攻略
- .net平台推送ios消息的实现方法
- 探讨Android与iOS,我们将何去何从?
- 基于jQuery的判断iPad、iPhone、Android是横屏还是竖屏的代码
- Android判断屏幕是横屏或是竖屏的简单实现方法
- Android、iOS和Windows Phone中的推送技术详解
- IOS 改变键盘颜色代码
- Android和IOS的浏览器中检测是否安装某个客户端的方法
- javascript实现阻止iOS APP中的链接打开Safari浏览器
- iOS开发之路--微博OAuth授权_取得用户授权的accessToken
- ios通过按钮点击异步加载图片