整个项目是竖屏的,不能横屏,但是有个播放界面必须要横屏于是就开始找各种横屏的方法,最后在手机上好使了,但是在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];
}

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 ...

最新文章

  1. python是一种面向对象的高级语言_爬了世纪佳缘后发现了一个秘密,世纪佳缘找对象靠谱吗?...
  2. 洛谷P3122 [USACO15FEB]圈住牛Fencing the Herd(计算几何+CDQ分治)
  3. ubuntu如何打开命令行
  4. 有了这份程序员面试指南,你离大厂 Offer 还远吗?| 附推荐书籍
  5. linux查看mongo表大小,MongoDB_mongodb 查看数据库和表大小,1,查看数据库db.stats() - phpStudy...
  6. 生成方法中参数的注释
  7. cucumber测试_如何在Cucumber中进行后端测试
  8. java wrap方法_Java WritableCellFormat.setWrap方法代码示例
  9. Android File.listFiles()返回null问题
  10. 小白也能懂的 Nacos 服务模型介绍
  11. Java主函数要放在哪个类里_JAVA:主函数一定要放在静态内部类里吗
  12. android 键盘点击事件监听事件,Android 键盘事件触发以及监听
  13. flask-mail异步发送邮件_Python爬虫系列:用邮件来通知爬虫异常状况
  14. mysql固定某列获取不连续的值_SQL-怎么把一列不规律的值,取出其中连续段的首尾数字?...
  15. 如何从GET参数获取值?
  16. python判断ip地址是否合法_Python课堂:判定IP地址合法性的三种方法
  17. java远古时代-驯龙骑士破解版_驯龙骑士apk下载_驯龙骑士安卓游戏v1.12.0-游迅网...
  18. 关于namecheap 域名运营商,域名赎回详细步骤
  19. 如何通过 6 个简单步骤在百度上看到您的网站
  20. VCS+dve+verdi仿真

热门文章

  1. [Linux 高并发服务器]GDB调试
  2. 锂电池充电电路及电源自动切换电路的设计
  3. JavaScript案例:H5 video自定义控制器
  4. xlwings模块学习
  5. JPQL和SQL的比较
  6. MobaXterm配置py虚拟环境及运行py脚本
  7. Second season eleventh episode,the lesbian wedding!!!
  8. 跨行成为一名程序员必备这5步,从程序员到工程师!
  9. 2021洪湖一中高考成绩查询,2021湖北省高中排名一览表
  10. 配置openfalcon监控kafka lag堆积情况