iOS强制横屏
来源:互联网 发布:杭州seo网络优化公司 编辑:程序博客网 时间:2024/05/22 04:50
在做视频播放时需要视频播放页面强制横屏,其他页面依然只支持竖屏,下面是使用过的两种方式。
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选择框显示依然是竖屏方向,改用了第二种方式,如果可以最好使用第二种方式。
0 0
- ios强制横屏
- iOS 强制横屏
- iOS强制横屏
- iOS强制横屏
- ios强制横屏
- iOS强制横屏
- iOS 6.0 强制横屏
- 关于iOS强制横屏
- IOS开发 强制横屏
- iOS强制横屏总结
- IOS开发——强制横屏/强制竖屏
- iOS强制切换横屏、竖屏
- iOS强制切换横屏、竖屏
- iOS 强制转成横屏的方式
- iOS 强制转成横屏的方式
- iOS 强制转成横屏的方式
- ios 相机界面强制横屏
- iOS VC的强制横屏
- onWindowFocusChanged重要作用 and Activity生命周期
- Msql和SQL server时间类型
- Google Java编程风格指南(献给那些没有良好编码习惯的程序员们)
- 移动开发UI设计、素材网站
- UVA Triangle Counting 11401【几何+数学】
- iOS强制横屏
- WCF和ASP.NET Web API
- 日经社説 20150818 天津の惨事を徹底究明せよ
- iOS开发--点击屏幕获得屏幕坐标
- 淘宝小故事:一个大型MySQL分布式系统诞生
- Java中抽象的总结
- EGOTableViewPullRefresh实现下拉刷新
- 获取键盘鼠标不动的时间
- 【MFC】创建显示对话框的DLL(共享DLL问题分析解决)