iOS界面设置竖屏,部分界面支持横竖屏

背景:

公司项目里要接入全景播放器,但是项目里所有的界面都只支持竖屏,但是播放器技术总监要求我必须能够设置播放器横屏播放。所以我在网络上找了很多前人的解决方法,但大都不能满足需求,然而也有可以用的方法可以使用,特粘贴出来供大家使用。

一、在视图管理器中设置横竖屏

此方法只适用于window的rootViewController为ViewController

-(BOOL)shouldAutorotate { return YES; } -(NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskAllButUpsideDown; }

二、在视图管理器中设置特定视图的横竖屏

此方法只适用于window的rootViewController为NavgationController或TabbarController

tab中:
-(BOOL)shouldAutorotate{ return [[self.viewControllers objectAtIndex:(int)self.selectedIndex] shouldAutorotate]; } -(UIInterfaceOrientationMask)supportedInterfaceOrientations{ return [[self.viewControllers objectAtIndex:(int)self.selectedIndex] supportedInterfaceOrientations]; } -(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{ return [[self.viewControllers objectAtIndex:(int)self.selectedIndex] preferredInterfaceOrientationForPresentation]; }
nav中:
-(BOOL)shouldAutorotate{ return [self.viewControllers.lastObject shouldAutorotate]; } -(UIInterfaceOrientationMask)supportedInterfaceOrientations{ return [self.viewControllers.lastObject supportedInterfaceOrientations]; } -(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{ return [self.viewControllers.lastObject preferredInterfaceOrientationForPresentation]; }

然后在特定的viewController中实现方法一

三、建立单例对象管理全局的横竖屏

只需要设置Appdelegate控制视图横竖屏的代理方法具体实现方法如下:

先在app delegate里加上下面这个方法
-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{ return [[VRScreenTool sharedControl] mask]; }
然后创建个单例类VRScreenTool全局进行调用
@interface VRScreenTool : NSObject @property (nonatomic, assign) UIInterfaceOrientationMask mask; +(instancetype)sharedControl; -(void)setOrientation:(UIInterfaceOrientationMask)mask; @end
@implementation VRScreenTool +(instancetype)sharedControl{ static VRScreenTool *instance; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ instance = [[self alloc] init]; }); return instance; } -(UIInterfaceOrientationMask)mask{ if (!_mask) { _mask = UIInterfaceOrientationMaskPortrait; } return _mask; } -(void)setOrientation:(UIInterfaceOrientationMask)mask { _mask = mask; } @end
然后在需要更改横竖屏的界面进行引用修改就可以了
-(void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [[VRScreenTool sharedControl] setOrientation:UIInterfaceOrientationMaskAllButUpsideDown]; } -(void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [[VRScreenTool sharedControl] setOrientation:UIInterfaceOrientationMaskPortrait]; }

当然你也可以直接调用appdelegte直接使用代理方法,但是个人不建议这样使用。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1.监听屏幕旋转方向 在处理iOS横竖屏时,经常会和UIDeviceOrientation、UIInterface...
    彬至睢阳阅读 2,568评论 1 6
  • 最近有一个项目,例如:A界面跳转到B界面,A界面是竖屏的,B界面进入就要横屏。 花了半天的时间在网上搜索解决方案,...
    手中的风信子阅读 20,851评论 6 29
  • iOS 中横竖屏切换的功能,在开发iOS app中总能遇到。以前看过几次,感觉简单,但是没有敲过代码实现,最近又碰...
    零度_不结冰阅读 2,253评论 0 0
  • 一、横竖屏有控制优先级,一旦优先级高的关闭了横竖屏配置,优先级低的无论如何配置都无法做到横竖屏。 对于限于VC范围...
    EdenMa阅读 391评论 0 0
  • 数据压缩 概况 本章描述了几种数据压缩选项,它能够帮助你减少磁盘空间的使用,在某种情况下,还可以提高I/O性能。 ...
    LestatZ阅读 1,299评论 0 1