https://www.jianshu.com/p/ea1682e80003

先讲需求:

APP中所有界面支持竖屏,只有在一个界面,点击一个btn之后变成横屏,再点就是竖屏。
在网上找了一些方法,发现实现不了,遂问了一个做过这个功能的朋友,得到朋友的支持之后,顺利解决这一问题

 
在这里因为是要全局界面都是支持竖屏,其他的不支持,在Xcode里面的配置

 
Xcode配置

然后就是在AppDelegate中添加属性和方法,

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
//是否强制横屏
@property (nonatomic, assign) BOOL isForceLandscape;
//是否强制竖屏
@property (nonatomic, assign) BOOL isForcePortrait; @end
添加属性
 
在AppDelegate中添加方法 
-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
if (self.isForceLandscape) {
return UIInterfaceOrientationMaskLandscape;
}else if (self.isForcePortrait){
return UIInterfaceOrientationMaskPortrait;
}else{
return UIInterfaceOrientationMaskPortrait;
}
}

一下是viewController中,即需要转换屏幕方向的.m文件的代码:

#import "ViewController.h"
#import "AppDelegate.h"
@interface ViewController () @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib. // _currentOrient = [UIApplication sharedApplication].statusBarOrientation; UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:@"转一转" forState:UIControlStateNormal];
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[button addTarget:self action:@selector(changeFrame:) forControlEvents:UIControlEventTouchUpInside];
button.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:button]; NSLayoutConstraint * centerX = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0];
NSLayoutConstraint * centerY = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0];
NSLayoutConstraint * width = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:100];
NSLayoutConstraint * height =[NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:44]; [NSLayoutConstraint activateConstraints:@[centerX,centerY,width,height]];
centerY.active = YES;
centerX.active = YES;
width.active = YES;
height.active = YES; }
// 允许自动旋转
-(BOOL)shouldAutorotate{
return YES;
}
// 横屏时是否将状态栏隐藏
-(BOOL)prefersStatusBarHidden{
return NO;
}
-(void)changeFrame:(UIButton *)btn{ btn.selected = !btn.selected; if (btn.selected) {
[self forceOrientationLandscapeWith:self];
}else{
[self forceOrientationPortraitWith:self];
}
}
// 横屏 home键在右边
-(void)forceOrientationLandscapeWith:(UIViewController *)VC{ AppDelegate *appdelegate=(AppDelegate *)[UIApplication sharedApplication].delegate;
appdelegate.isForcePortrait=NO;
appdelegate.isForceLandscape=YES;
[appdelegate application:[UIApplication sharedApplication] supportedInterfaceOrientationsForWindow:VC.view.window]; //强制翻转屏幕,Home键在右边。
[[UIDevice currentDevice] setValue:@(UIInterfaceOrientationLandscapeRight) forKey:@"orientation"];
//刷新
[UIViewController attemptRotationToDeviceOrientation];
}
// 竖屏
- (void)forceOrientationPortraitWith:(UIViewController *)VC{ AppDelegate *appdelegate=(AppDelegate *)[UIApplication sharedApplication].delegate;
appdelegate.isForcePortrait=YES;
appdelegate.isForceLandscape=NO;
[appdelegate application:[UIApplication sharedApplication] supportedInterfaceOrientationsForWindow:VC.view.window]; //强制翻转屏幕
[[UIDevice currentDevice] setValue:@(UIDeviceOrientationPortrait) forKey:@"orientation"];
//刷新
[UIViewController attemptRotationToDeviceOrientation];
}

这样就实现了点击切换屏幕方向的需求,嘻嘻!!

iOS界面横屏竖屏随意切换的更多相关文章

  1. activity横屏竖屏的切换

    原理: 其实总结起来,我们可以得到以下的一些结论 1.当内存不足(不容易模拟).切屏时会调用onSaveInstanceState().onRestoreInstanceState()方法 对于onS ...

  2. cocos2d-x ios 设置横屏/竖屏(全)

    Cocos2d-x项目\iOS\RootViewController.mm文件中. 以下方法任选其一即可…      本人机子函数二ok! 函数一: (BOOL)shouldAutorotateToI ...

  3. 【转】Android 模拟器横屏竖屏切换设置

    http://blog.csdn.net/zanfeng/article/details/18355305# Android 模拟器横屏竖屏切换设置时间:2012-07-04   来源:设计与开发   ...

  4. android界面横屏和竖屏的切换

    关于android横屏和竖屏的切换网上给了很多种.但是有些介绍的方法都是在android旧版本上. 我现在把握用到的情况写下来用于备忘: android 版本:4.0 - 4.4 要求:android ...

  5. Android横屏竖屏切换的问题

    Android横屏竖屏切换的问题 http://blog.sina.com.cn/s/blog_77c632410101790w.html

  6. Android——横屏和竖屏的切换,以及明文密码的显示

    查看API文档: android.content.pm.ActivityInfo    在手机的使用中,我们要根据不同的需求来改变屏幕的显示方向,一般在浏览信息时是竖屏,在玩游戏的时候就要切换到横屏. ...

  7. vue手机端横屏竖屏切换

    1.建一个空白的vue文件,添加上如下代码 data() { this.$router.go(-1) return {} }   2.在需要横屏竖屏切换的页面中加入如下代码: beforeMount( ...

  8. js 横屏 竖屏 相关代码 与知识点

    <!DOCTYPE html> <html> <head> <title></title> </head> <body&g ...

  9. Android之横屏竖屏显示问题

    1.採用不同的布局文件 在res下创建目录layout-land 在该目录下写入的layout(xml文件)横屏的时候系统自己主动选择显示的layout 同理: 在res下创建目录layout-por ...

  10. HTML5中判断横屏竖屏

    在移动端中我们经常碰到横屏竖屏的问题,那么我们应该如何去判断或者针对横屏.竖屏来写不同的代码呢. 这里有两种方法: 一:CSS判断横屏竖屏 写在同一个CSS中 1 2 3 4 5 6 @media s ...

随机推荐

  1. vue面试题及答案(1)

    vue面试题,知识点汇总(有答案) 一. Vue 核心小知识点 1.vue 中 key 值的作用 key     的特殊属性主要用在 Vue 的虚拟 DOM 算法,在新旧 nodes 对比时辨识 VN ...

  2. Chrome浏览器提示您的连接不是私密连接解决办法

    解决方案: 是在当前页面用键盘输入 thisisunsafe ,不是在地址栏输入,就直接敲键盘就行了,页面即会自动刷新进入网页. 原因: 因为Chrome不信任这些自签名ssl证书,为了安全起见,直接 ...

  3. @Configuration 配置类打断点后,一启动项目读取到该配置类的话就会进断点

    @Configuration 配置类的话,打断点的时候,一启动项目就会读取配置信息,然后你在@Configuration 配置的类中打断点的话,一启动项目就会读取配置类,然后就会进断点,跟你平常的co ...

  4. 使用 docker-compose 安装 es、kibana、mysql、redis、kafka

    由于经常的安装虚拟机,每次都要重装环境比较麻烦. 这次写了一个 docker-compose 文件,一次性搞定安装问题. 目录结果如下: --- version: "3.1" ne ...

  5. pytest-2 之前后置及 conftest.py+fixture+yield实现用例前后置

    pytest测试用例及类级别的前置,可以和unittest一样进行定义,也可以把该前置方法或类定义到conftest.py里,而在需要前置的方法的参数里加上该前置名作为参数: pytest有两种方式来 ...

  6. 使用ADB拷贝Android设备的文件夹

    在当前目录下执行,拷贝到当前目录.   拷贝照片 adb pull sdcard/DCIM   删除照片 adb shell rm -rvf sdcard/DCIM   拷贝图片 adb pull s ...

  7. 读后笔记 -- Python 全栈测试开发 Chapter10:接口的设计与开发

    10.1 Django 框架 1. 几个主流的框架: 1)适合初学者的接口框架:Django,Flask 2)针对底层定义:Twisted 3)实现高并发:Tornado 2. install // ...

  8. Ios安装charles

    1.浏览器打开chls.pro/ssh,不会弹出下载证书,所以使用以下方式: 2. 然后打开设置,可以看到描述文件,安装 检查:在"设置"-"描述文件"-查看描 ...

  9. HarmonyOS基础

    目录 自适应布局 自适应拉伸布局 自适应缩放 自适应延伸 组件多态 ArkUI开发框架 基础组件 Text组件和Span组件 参考 参考:harmonyos3: 鸿蒙ArkUI eTS教程配套源码 参 ...

  10. SAP 结构转JSON

    *使用方式 jsonstr = zui2_json=>serialize( data = ls_in compress = abap_true pretty_name = zui2_json=& ...