整个项目是竖屏的,不能横屏,但是有个播放界面必须要横屏于是就开始找各种横屏的方法,最后在手机上好使了,但是在pad上横屏启动的时候界面是横屏显示,很是苦恼,就又开始了漫长的找资料,直接上代码

1,配置plist文件和deployemnt Info -> device orientation

(1),plist文件,如图所示,第一项是建立项目时默认有的表示支持手机的屏幕方向(我把支持向右和向左的删了),第二项是后加的表示支持ipad(添加 Supported interface orientations (iPad))的旋转方向(同样我把向左向右,向上的给删了)

(2),deployemnt Info -> device orientation,只选择第一项竖屏。

2,开始代码配置(主要讲解push界面的强制横屏)主要是借鉴简书http://www.jianshu.com/p/5c773628caa6这位大神的讲解

(1),AppDelegate.h里面

@property (assign , nonatomic) BOOL isForceLandscape;
@property (assign , nonatomic) BOOL isForcePortrait;

AppDelegate.m里面

-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{if (self.isForceLandscape) {return UIInterfaceOrientationMaskLandscape;}else if (self.isForcePortrait){return UIInterfaceOrientationMaskPortrait;}return UIInterfaceOrientationMaskPortrait;
}

(2),在相应的tabBarController里面(我是在viewController里面写的)

#import <UIKit/UIKit.h>
#import "BaseViewController.h"@interface RootViewController : BaseViewController@property (strong,nonatomic) UITabBarController *tabBarCon;
@end
/// 选择的当前控制器是否可以旋转
-(BOOL)shouldAutorotate{return [self.tabBarCon.selectedViewController shouldAutorotate];
}/// 选择的当前控制器是支持的旋转的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{return  [self.tabBarCon.selectedViewController supportedInterfaceOrientations];
}///
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{return  [self.tabBarCon.selectedViewController preferredInterfaceOrientationForPresentation];
}

(3),在navigationController里面(自己创建的navigationController的子类)

#import <UIKit/UIKit.h>@interface RootNavigationController : UINavigationController//旋转方向 默认竖屏
@property (nonatomic , assign) UIInterfaceOrientation interfaceOrientation;
@property (nonatomic , assign) UIInterfaceOrientationMask interfaceOrientationMask;
/// 当前的导航控制器是否可以旋转
-(BOOL)shouldAutorotate{return YES;
}//设置支持的屏幕旋转方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {return self.interfaceOrientationMask;
}//设置presentation方式展示的屏幕方向
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {return self.interfaceOrientation;
}

(4)在父类viewController里面

- (void)viewDidLoad {[super viewDidLoad];// 表示本类支持旋转[UIViewController attemptRotationToDeviceOrientation];
}

(5),在需要强制横屏的地方调用

- (void)viewWillAppear:(BOOL)animated{self.navigationController.navigationBar.hidden = YES;self.navigationController.tabBarController.tabBar.hidden = YES;// 强制横屏[self forceOrientationLandscape];RootNavigationController *nav = (RootNavigationController *)self.navigationController;nav.interfaceOrientation = UIInterfaceOrientationLandscapeRight;nav.interfaceOrientationMask = UIInterfaceOrientationMaskLandscapeRight;//强制翻转屏幕,Home键在右边。[[UIDevice currentDevice] setValue:@(UIInterfaceOrientationLandscapeRight) forKey:@"orientation"];//刷新[UIViewController attemptRotationToDeviceOrientation];
}- (void)viewWillDisappear:(BOOL)animated{//强制旋转竖屏[self forceOrientationPortrait];RootNavigationController *navi = (RootNavigationController *)self.navigationController;navi.interfaceOrientation = UIInterfaceOrientationPortrait;navi.interfaceOrientationMask = UIInterfaceOrientationMaskPortrait;//设置屏幕的转向为竖屏[[UIDevice currentDevice] setValue:@(UIDeviceOrientationPortrait) forKey:@"orientation"];//刷新[UIViewController attemptRotationToDeviceOrientation];
}
#pragma  mark 横屏设置
//强制横屏
- (void)forceOrientationLandscape{AppDelegate *appdelegate=(AppDelegate *)[UIApplication sharedApplication].delegate;appdelegate.isForceLandscape=YES;appdelegate.isForcePortrait=NO;[appdelegate application:[UIApplication sharedApplication] supportedInterfaceOrientationsForWindow:self.view.window];
}//强制竖屏
- (void)forceOrientationPortrait{AppDelegate *appdelegate=(AppDelegate *)[UIApplication sharedApplication].delegate;appdelegate.isForcePortrait=YES;appdelegate.isForceLandscape=NO;[appdelegate application:[UIApplication sharedApplication] supportedInterfaceOrientationsForWindow:self.view.window];
}

总结,感觉http://www.jianshu.com/p/5c773628caa6这位大神已经总结的很到位了,希望可以帮到需要的人

ios 强制横屏大总结相关推荐

  1. php页面强制横屏,iOS强制横屏总结

    总体方向两点: model下,present方式推出界面. push横屏,带tabbar.navigation,且一个item下所有控制器对应的只有一个根navigation. 接下来说说push方式 ...

  2. Android开发之强制横屏和强制竖屏

    原文地址:https://blog.csdn.net/qq_37219980/article/details/71194313 强制竖屏设置 1.代码在Activity的onResume方法中添加如下 ...

  3. Flutter android及ios强制竖屏/横屏

    Flutter android及ios强制竖屏/横屏 在main.dart内设置即可 在main.dart内设置即可 void main(){WidgetsFlutterBinding.ensureI ...

  4. iOS设置某个界面强制横屏,进入就横屏

    原文地址:http://www.cnblogs.com/niit-soft-518/p/5611298.html 方案一: 使用 presentViewController 1.首先设置项目 支持的屏 ...

  5. ios中html怎么横屏,iOS如何实现强制转屏、强制横屏和强制竖屏的实例代码

    本文介绍了iOS如何实现强制转屏.强制横屏和强制竖屏的实例代码,分享给大家 今天项目中遇到正在看视频的时候账号被挤,如果当时是横屏的情况下,需要强制竖屏.真头疼,网上找了好多方法,终于解决啦.O(∩_ ...

  6. 高德地图强制横屏后(旋转90deg)在手机上拖动方向问题(vue)

    大屏需要在手机端横屏展示,强制横屏后,地图的拖动方向也相对正常的方向旋转90度 代码片段 html部分 <divid="mapInit"@touchstart.prevent ...

  7. html 静止横屏,CSS实现Html页面强制横屏

    说起来html页面强制横屏,最多的都是js实现html页面强制横屏,用css实现这个功能的,很少人知道,那么今天就谈一谈如何用CSS实现html页面横屏显示,无论用户怎么旋转手机都是横屏!!! 场景分 ...

  8. 移动端横屏/强制横屏

    整体演示: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  9. 竖屏下进入强制横屏应用,退出时壁纸被拉伸放大

    问题现象 手机保持竖屏,进入强制横屏的应用,退出时壁纸被拉伸放大显示. 横屏时进入强制竖屏应用退出时同样会放大. 再次旋转一下手机,可以恢复正常. 问题分析 壁纸的显示在SystemUI的com/an ...

  10. iOS-强制横屏大总结

    整个项目是竖屏的,不能横屏,但是有个播放界面必须要横屏于是就开始找各种横屏的方法,最后在手机上好使了,但是在pad上横屏启动的时候界面是横屏显示,很是苦恼,就又开始了漫长的找资料,直接上代码 1.配置 ...

最新文章

  1. Linux终端运行fasterrcnn,对yolo与fasterrcnn anchors的理解
  2. Swift 文档读后随写
  3. SpringCloud Openfeign
  4. [html] 如何实现标题栏闪烁、滚动的效果
  5. 前端学习(2077):开始回顾
  6. 34步优化sql语句
  7. 1.RESTful Web APIs中文版 --- 网上冲浪
  8. python吃显卡还是内存条_用游戏本打游戏是显卡重要还是内存重要?
  9. 【数据分析】基于matlab GUI kmeans聚类分组系统【含Matlab源码 510期】
  10. 使用命令行提取微软补丁文件
  11. ASUS TUF Gaming B460M PLUS+RX570+BCM94360CD黑苹果EFI引导文件
  12. 微信小程序大家来找茬 ,快速通过方法
  13. docker 安装mysql8.0并且暴漏外部的连接
  14. 开关电源原理、电路组成部分
  15. 莫名其妙出现各种syntax error的解决方案
  16. 【python数据分析(24)】Matplotlib库基本图形绘制(1)(线形图、柱状图、堆叠图、面积图、填图、饼图)
  17. C++读取bmp格式图片
  18. 会声会影2022新版本视频图文教程介绍
  19. 教你疾速驾驭DB2数据库中的相关饬令-3
  20. 排球制作html,3DMAX简单制作一个真实的排球效果图

热门文章

  1. python数据挖掘14讲_python/pandas数据挖掘(十四)-groupby,聚合,分组级运算
  2. flyway 社区版本使用团队(企业级)特性ignore-migration-patterns使用
  3. 重量级ORM框架--持久化框架Hibernate【关系映射详解】
  4. Android做加入购物车,Android实现购物车添加商品动画
  5. 开启充电计划升级职业生涯,让我们相聚社科院杜兰金融管理硕士项目
  6. 上海国拍服务器位置,上海国拍系统的重大漏洞
  7. 万王之王手游服务器维护,万王之王手游-KOK-官方网站-腾讯游戏-一个世界的重新开启...
  8. 如何使用REST Assured执行API测试
  9. canvas生成gif图
  10. 对Airtest报告的步骤标题做内容定制?实用速学