切换横竖屏

oc版本

/// 旋转屏幕
/// - Parameters:
///   - interfaceOrientation: 目标屏幕方向
///   - viewController: 所在的视图控制器
///   - errorHandler: 错误回调
+ (void)rotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation viewController:(UIViewController *)viewController errorHandler:(void (^)(NSError *))errorHandler {#if __IPHONE_16_0 //兼容 Xcode13if (@available(iOS 16.0, *)) {UIWindowScene *windowScene = viewController.view.window.windowScene;if (!windowScene) {return;}[viewController setNeedsUpdateOfSupportedInterfaceOrientations];UIWindowSceneGeometryPreferencesIOS *geometryPreferences = [[UIWindowSceneGeometryPreferencesIOS alloc] init];switch (interfaceOrientation) {case UIInterfaceOrientationPortrait:geometryPreferences.interfaceOrientations = UIInterfaceOrientationMaskPortrait;break;case UIInterfaceOrientationPortraitUpsideDown:geometryPreferences.interfaceOrientations = UIInterfaceOrientationMaskPortraitUpsideDown;break;case UIInterfaceOrientationLandscapeLeft:geometryPreferences.interfaceOrientations = UIInterfaceOrientationMaskLandscapeLeft;break;case UIInterfaceOrientationLandscapeRight:geometryPreferences.interfaceOrientations = UIInterfaceOrientationMaskLandscapeRight;break;default:break;}[windowScene requestGeometryUpdateWithPreferences:geometryPreferences errorHandler:^(NSError * _Nonnull error) {//业务代码NSLog(@"menglc errorHandler error %@", error);if (errorHandler) {errorHandler(error);}}];return;}
#endifif([[UIDevice currentDevice]respondsToSelector:@selector(setOrientation:)]) {SEL selector = NSSelectorFromString(@"setOrientation:");NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];[invocation setSelector:selector];[invocation setTarget:[UIDevice currentDevice]];int val = UIInterfaceOrientationLandscapeRight;//横屏UIInterfaceOrientationPortrait[invocation setArgument:&val atIndex:2];[invocation invoke];}
}

Swift版本

/// 切换横竖屏public static func rotate(to toInterfaceOrientation: UIInterfaceOrientation, viewController:UIViewController) {#if __IPHONE_16_0 //兼容 Xcode13if #available(iOS 16, *) {guard let windowScene = viewController.view.window?.windowScene else {return}viewController.setNeedsUpdateOfSupportedInterfaceOrientations()let interfaceOrientation: UIInterfaceOrientationMaskswitch toInterfaceOrientation {case .unknown, .portrait:interfaceOrientation = .portraitcase .portraitUpsideDown:interfaceOrientation = .portraitUpsideDowncase .landscapeLeft:interfaceOrientation = .landscapeLeftcase .landscapeRight:interfaceOrientation = .landscapeRight@unknown default:interfaceOrientation = .portrait}let geometryPreferences = UIWindowScene.GeometryPreferences.iOS(interfaceOrientations: interfaceOrientation)windowScene.requestGeometryUpdate(geometryPreferences) { error in}return}
#endifguard let selector = Selector(("setOrientation")) as Selector?, UIDevice.current.responds(to: selector), let methodSignature = UIDevice.instanceMethod(for: selector) else {return}// todo}

获取当前屏幕横竖屏状态

oc版本

+ (UIInterfaceOrientation)interfaceOrientation {// 获取当前屏幕横竖屏状态if (@available(iOS 13.0, *)) {return [UIApplication sharedApplication].windows.firstObject.windowScene.interfaceOrientation;}return [[UIApplication sharedApplication] statusBarOrientation];
}

Swift版本

/// 获取当前屏幕横竖屏状态
public class func interfaceOrientation() -> UIInterfaceOrientation {guard #available(iOS 13, *) else {return UIApplication.shared.statusBarOrientation}return UIApplication.shared.windows.first?.windowScene?.interfaceOrientation ?? .unknown}

iOS 16横竖屏切换适配相关推荐

  1. iOS 中横竖屏切换

    iOS 中横竖屏切换的功能,在开发iOS app中总能遇到.以前看过几次,感觉简单,但是没有敲过代码实现,最近又碰到了,demo尝试了几种情况,这里就做下总结. 注意 横屏两种情况是反的你知道吗? U ...

  2. iOS终极横竖屏切换解决方案

    大家的项目都是只支持竖屏的吧?大多数朋友(这其中当然也包括博主),都没有做过横屏开发,这次项目刚好有这个需求,因此把横竖屏相关的心得写成一遍文章供诸位参考. 01.综述 大多数公司的项目都只支持竖屏, ...

  3. iOS的横竖屏切换旋转(禁自动旋转)

    这次做了视频的播放器,坑啊 ,好多,这不,刚刚爬上来,就来帮后来者填坑... 首先先说下横竖屏切换旋转的坑吧,,, 1. 在AppDelegate.h文件中 声明一个变量, @property (no ...

  4. [贝聊科技] iOS 终极横竖屏切换解决方案

    大家的项目都是只支持竖屏的吧?大多数朋友(这其中当然也包括博主),都没有做过横屏开发,这次项目刚好有这个需求,因此把横竖屏相关的心得写成一遍文章供诸位参考. 01.综述 大多数公司的项目都只支持竖屏, ...

  5. iOS视频播放横竖屏切换技巧

    一.需求:横竖屏切换. 二.效果:                       三.实现: 如上图,点击工具栏的第四个按钮进行横屏切换: - (void)toolTabButtonPressed:(A ...

  6. iOS 6横竖屏切换

    iOS6.0版本之前,UIViewController之间的横竖屏切换,只需设置一个函数: - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInte ...

  7. iOS应用横竖屏切换

    一.概述:     在iOS应用中,由UIViewController来控制屏幕翻转,根据需要随设备方向自动切换.在iOS6和之前的系统之间,控制方法发生了些变化. 二.视图伸缩属性: 1.UIVie ...

  8. ios 旋转屏幕试图切换_iOS屏幕横竖屏切换

    iOS屏幕横竖屏切换 胡东东博客 • 2020 年 10 月 25 日 搜了网上的教程是真的乱,废话不多说,这里从启动到具体的VC,横竖屏切换完美搞定. 如果你的app只需要支持一个方向,那么不需要看 ...

  9. iOS 横竖屏切换解决方案

    iOS 横竖屏切换解决方案 参考文章: (1)iOS 横竖屏切换解决方案 (2)https://www.cnblogs.com/qqcc1388/p/7358552.html 备忘一下.

  10. 完美解决三星手机拍照后横竖屏切换导致的回调为null以致程序崩溃的问题(包括三星note3的特殊适配)

    场景:安卓开发中我们经常会有拍照上传的需求,比如上传图片,或者上传头像等等,方法也比较常规,通过调用startActivityForResult(Intent intent, int requestC ...

最新文章

  1. 想做数据分析?这个比赛适合你!
  2. sola ris 简单命令
  3. 特斯拉烧,特斯拉烧完蔚来烧
  4. 6kzz整合ueditor
  5. 笔记整理-信息系统工程监理(四控三管一协调)-监理规划、监理实施细则
  6. mysql 复制诊断_诊断Mysql复制问题
  7. 自然语言12_Tokenizing Words and Sentences with NLTK
  8. kafka启动_Kafka安装部署——单节点
  9. ul 原点显示_CSS+HTML ul li列表原点如何相连
  10. Vue实现一个按钮切换显示不同的div内容
  11. 零基础学python书籍-图书推荐:《零基础学Python(全彩版)》
  12. 为什么要进行网络的分层?
  13. Android--读取通讯录并添加联系人
  14. ubuntu安装vmware-tools
  15. 字符集本地化(locale)与输入法系列讲座-----(1) UTF-8 and Unicode FAQ
  16. C# 中intptr用法
  17. 上海市计算机二级vb试题及答案,上海市207计算机二级vb试题.doc
  18. 电脑自动跳转加QQ好友html,自动添加QQ好友.html
  19. win10系统下插入U盘有声音提示却不显示盘符
  20. docker出现 Error starting userland proxy: listen tcp4 0.0.0.0:3306: bind: address already in use的解决方法

热门文章

  1. win7计算机名称格式,win7笔记本电脑如何显示文件扩展名
  2. matlab图片背景分割,12.4.2 图像分割
  3. java近义词,java实现近义词维护
  4. Mac 没有声音怎么恢复
  5. 英语学习详细笔记(九)分词
  6. win10系统禁用音频服务器,Win10下怎样设置禁用扬声器、插入耳机有声音【图文教程】...
  7. Zookeeper的事务--Transaction
  8. 96309245通讯异常工行_工商银行信息代码 96309245 是什么意思
  9. 如何把 Mac 中的文件拷贝到NTFS硬盘?
  10. linux ps查看进程,Linux命令之ps:查看进程状态