在写项目的时候,会遇到很多稀奇古怪的需求,我就碰到一个写一个网站,需要强制横屏,然后不需要上架,网上看了很多大神的需求,基本都能实现,但是不太好用,

自己参考搞了一个,代码如下:

AppDelegate.h

@property(nonatomic,assign)BOOL allowRotation;//是否允许转向

.m

#pragma mark 支持窗口翻转
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window { if (_allowRotation == YES) { return UIInterfaceOrientationMaskLandscapeRight; }else{ return (UIInterfaceOrientationMaskPortrait); } }

横屏展示的viewcontroler:

.m

- (void)setNewOrientation:(BOOL)fullscreen

{

    if (fullscreen) {

        NSNumber *resetOrientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];

        [[UIDevice currentDevice] setValue:resetOrientationTarget forKey:@"orientation"];

        NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];

        [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];

    }else{

        NSNumber *resetOrientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];

        [[UIDevice currentDevice] setValue:resetOrientationTarget forKey:@"orientation"];

        NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];

        [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];

    }

}

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib. AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate; appDelegate.allowRotation = YES;//(以上2行代码,可以理解为打开横屏开关) [self setNewOrientation:YES];//调用转屏代码 [self creatWebView]; } -(void)creatWebView
{
UIWebView *webV = [[UIWebView alloc]initWithFrame:self.view.frame];
[webV loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.badiu.com"]]];
[self.view addSubview:webV];
}

这样的话,进入的vc直接就是横屏展示了,我是在这定死了,只能向右横屏展示,这个可以自己设置的,根据重力展示,如图:

在上架箭头的地方设置即可!

最新文章

  1. Linux下文件的三种时间戳
  2. 本地项目上传到GitHub
  3. ftp应用
  4. DataGridView 导出到Excel
  5. 可在广域网部署运行的QQ高仿版 -- GG2014 完美版!新增支持:聊天记录、好友分组、托盘闪动消息提醒、登录状态、GIF动态表情
  6. Java IO 装饰者模式
  7. MongoDB学习笔记——Master/Slave主从复制
  8. Mac下搭建php开发环境教程
  9. 四项技术 助你提高SQL Server的性能
  10. 《ASP.NET MVC4 WEB编程》学习笔记------Entity Framework的Database First、Model First和Code Only三种开发模式
  11. python & pandas链接mysql数据库
  12. post 方式提交XML文件调用接口
  13. Cadence OrCad Allegro SPB 16.6 下载及安装破解指南
  14. 20160728noip模拟赛zld
  15. C#使用SOAP获取webservice实例解析
  16. C51与汇编混合编程详解
  17. DOS和windows的区别?
  18. C语言系列之强制类型转换(一)
  19. python 判断连个 Path 是否是相同的文件夹
  20. 8、Spring-Kafka Recving Messages

热门文章

  1. mysql允许远程连接
  2. Redis全方位讲解--哨兵模式(Sentinel模式)(转载)
  3. 浅谈LZSS与游戏图片破解
  4. centos 搭建 搭建uwsgi服务
  5. IntelliJ IDEA 学习(二):Intellij IDEA 创建Web项目并在Tomcat中部署运行IDEA
  6. ubuntu环境下配置jdk
  7. chrome 浏览器插件开发
  8. MySQL replace into 用法
  9. RabbitMQ php 使用
  10. atitit.解决SyntaxError: missing ] after element list"不个object 挡成个str eval ....