iOS 控制每个vc横竖屏

server/2024/9/24 23:20:44/

此处引用的是ZFPlayer的里面的一个旋转控制类,此处手动感谢一下ZFPlayer的作者,受小弟一拜,感谢大神!!!

一、 思想:
  • 由于横竖屏的最终确定是由最跟控制器决定的,然而一般的APP结构就是uitablebarcontroller+uinavigationController+uiviewController,所以可以根据以上结构然后利用系统方法让最终决定横竖屏的地方转移到每个vc里面
二、 具体方法:

1、首先写一个UIViewController的分类,在里面把传递到vc的代码写好,也就是uitablebarcontroller+uinavigationController里面的系统方法重写一下

#import <UIKit/UIKit.h>
#import <objc/runtime.h>@implementation UITabBarController (ZFPlayerRotation)+ (void)load {static dispatch_once_t onceToken;dispatch_once(&onceToken, ^{SEL selectors[] = {@selector(selectedIndex)};for (NSUInteger index = 0; index < sizeof(selectors) / sizeof(SEL); ++index) {SEL originalSelector = selectors[index];SEL swizzledSelector = NSSelectorFromString([@"zf_" stringByAppendingString:NSStringFromSelector(originalSelector)]);Method originalMethod = class_getInstanceMethod(self, originalSelector);Method swizzledMethod = class_getInstanceMethod(self, swizzledSelector);if (class_addMethod(self, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod))) {class_replaceMethod(self, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));} else {method_exchangeImplementations(originalMethod, swizzledMethod);}}});
}- (NSInteger)zf_selectedIndex {NSInteger index = [self zf_selectedIndex];if (index > self.viewControllers.count) return 0;return index;
}/*** If the root view of the window is a UINavigationController, you call this Category first, and then UIViewController called.* All you need to do is revisit the following three methods on a page that supports directions other than portrait.*/// Whether automatic screen rotation is supported.
- (BOOL)shouldAutorotate {UIViewController *vc = self.viewControllers[self.selectedIndex];if ([vc isKindOfClass:[UINavigationController class]]) {UINavigationController *nav = (UINavigationController *)vc;return [nav.topViewController shouldAutorotate];} else {return [vc shouldAutorotate];}
}// Which screen directions are supported.
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {UIViewController *vc = self.viewControllers[self.selectedIndex];if ([vc isKindOfClass:[UINavigationController class]]) {UINavigationController *nav = (UINavigationController *)vc;return [nav.topViewController supportedInterfaceOrientations];} else {return [vc supportedInterfaceOrientations];}
}// The default screen direction (the current ViewController must be represented by a modal UIViewController (which is not valid with modal navigation) to call this method).
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {UIViewController *vc = self.viewControllers[self.selectedIndex];if ([vc isKindOfClass:[UINavigationController class]]) {UINavigationController *nav = (UINavigationController *)vc;return [nav.topViewController preferredInterfaceOrientationForPresentation];} else {return [vc preferredInterfaceOrientationForPresentation];}
}@end@implementation UINavigationController (ZFPlayerRotation)/*** If the root view of the window is a UINavigationController, you call this Category first, and then UIViewController called.* All you need to do is revisit the following three methods on a page that supports directions other than portrait.*/// Whether automatic screen rotation is supported
- (BOOL)shouldAutorotate {return [self.topViewController shouldAutorotate];
}// Which screen directions are supported
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {return [self.topViewController supportedInterfaceOrientations];
}// The default screen direction (the current ViewController must be represented by a modal UIViewController (which is not valid with modal navigation) to call this method).
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {return [self.topViewController preferredInterfaceOrientationForPresentation];
}- (UIViewController *)childViewControllerForStatusBarStyle {return self.topViewController;
}- (UIViewController *)childViewControllerForStatusBarHidden {return self.topViewController;
}@end

2、然后在每个vc里面就可以使用下面的代码单独设置强制横、竖屏,或者横竖屏随意切换

- (UIStatusBarStyle)preferredStatusBarStyle {if (self.player.isFullScreen) {return UIStatusBarStyleLightContent;}return UIStatusBarStyleDefault;
}- (BOOL)prefersStatusBarHidden {return self.player.isStatusBarHidden;
}- (UIStatusBarAnimation)preferredStatusBarUpdateAnimation {return UIStatusBarAnimationSlide;
}- (BOOL)shouldAutorotate {return self.player.shouldAutorotate;
}- (UIInterfaceOrientationMask)supportedInterfaceOrientations {return UIInterfaceOrientationMaskLandscape;
}- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {return UIInterfaceOrientationLandscapeRight;
}

http://www.ppmy.cn/server/4287.html

相关文章

阿里云们死磕芯片自研,不想沦为英特尔、英伟达们的“打工仔”

大数据产业创新服务媒体 ——聚焦数据 改变商业 在当今快速发展的数字经济时代&#xff0c;云计算已成为推动各行各业创新和转型的关键力量。作为云计算的核心组件&#xff0c;芯片的作用不言而喻。它不仅是服务器算力的源泉&#xff0c;更是云服务提供商提供高效、可靠服务的…

2024信友队智灵班春季 Test1 总结

4月模考 死亡回放 模考时间线 1:30 比赛开始&#xff0c;读 T1 宇宙爆炸 的题1:50 自己手模了几组样例&#xff0c;得出了一个错误结论&#xff0c;打出了第一版错误代码&#xff0c;然后上交&#xff08; Wrong Answer 20 \color{red}\text{Wrong\ Answer\ 20} Wrong Answer …

设计模式知识总结

单例模式 懒汉式 线程不安全的懒汉单例 class singleton { private:singleton() {}static singleton *p; public:static singleton *instance();void st(); }; singleton *singleton::p nullptr; singleton* singleton::instance() {if (p nullptr)p new singleton();re…

Vue3 Vite配置环境变量

Vue3 Vite配置环境变量 相关文档配置.env文件vite.config.jspackage.json 使用 相关文档 Vite 官方中文文档&#xff1a;https://cn.vitejs.dev/环境变量和模式&#xff1a;https://cn.vitejs.dev/guide/env-and-mode.html#env-file在配置中使用环境变量&#xff1a;https://c…

CentOS:执行make命令时报错g++: Command not found

报错截图&#xff1a; 其实很简单只需要安装一下 yum -y install gcc automake autoconf libtool make yum install gcc gcc-c

Stable Diffusion教程:LoRA模型

LoRA模型是一种微调模型&#xff0c;它不能独立生成图片&#xff0c;常常用作大模型的补充&#xff0c;用来生成某种特定主体或者风格的图片。 下载模型 在模型下载网站&#xff0c;如果模型是LoRA模型&#xff0c;网站会特别标识出来。以 liblib.ai为例&#xff1a; 模型左…

力扣 | 24. 两两交换链表中的节点

两两交换链表中的节点 给定一个链表&#xff0c;两两交换其中相邻的节点&#xff0c;并返回交换后的链表。 你不能只是单纯的改变节点内部的值&#xff0c;而是需要实际的进行节点交换。 输入&#xff1a;head 1->2->3->4->5->NULL 输出&#xff1a;2->1-&g…

【Spring进阶】基于注解的面向切面编程(AOP)详解

hi&#xff0c;我是程序员王也&#xff0c;一个资深Java开发工程师&#xff0c;平时十分热衷于技术副业变现和各种搞钱项目的程序员~&#xff0c;如果你也是&#xff0c;可以一起交流交流。 今天我们聊一聊Spring中的AOP~ AOP的核心概念 面向切面编程&#xff08;AOP&#xff…