ios开发之 -- 强制横屏
2024-09-28 07:11:32
在写项目的时候,会遇到很多稀奇古怪的需求,我就碰到一个写一个网站,需要强制横屏,然后不需要上架,网上看了很多大神的需求,基本都能实现,但是不太好用,
自己参考搞了一个,代码如下:
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直接就是横屏展示了,我是在这定死了,只能向右横屏展示,这个可以自己设置的,根据重力展示,如图:
在上架箭头的地方设置即可!
最新文章
- Linux下文件的三种时间戳
- 本地项目上传到GitHub
- ftp应用
- DataGridView 导出到Excel
- 可在广域网部署运行的QQ高仿版 -- GG2014 完美版!新增支持:聊天记录、好友分组、托盘闪动消息提醒、登录状态、GIF动态表情
- Java IO 装饰者模式
- MongoDB学习笔记——Master/Slave主从复制
- Mac下搭建php开发环境教程
- 四项技术 助你提高SQL Server的性能
- 《ASP.NET MVC4 WEB编程》学习笔记------Entity Framework的Database First、Model First和Code Only三种开发模式
- python &; pandas链接mysql数据库
- post 方式提交XML文件调用接口
- Cadence OrCad Allegro SPB 16.6 下载及安装破解指南
- 20160728noip模拟赛zld
- C#使用SOAP获取webservice实例解析
- C51与汇编混合编程详解
- DOS和windows的区别?
- C语言系列之强制类型转换(一)
- python 判断连个 Path 是否是相同的文件夹
- 8、Spring-Kafka Recving Messages
热门文章
- mysql允许远程连接
- Redis全方位讲解--哨兵模式(Sentinel模式)(转载)
- 浅谈LZSS与游戏图片破解
- centos 搭建 搭建uwsgi服务
- IntelliJ IDEA 学习(二):Intellij IDEA 创建Web项目并在Tomcat中部署运行IDEA
- ubuntu环境下配置jdk
- chrome 浏览器插件开发
- MySQL replace into 用法
- RabbitMQ php 使用
- atitit.解决SyntaxError: missing ] after element list";不个object 挡成个str eval ....