美文网首页00『 基础知识 』
iOS界面设置竖屏,部分界面支持横竖屏

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

作者: 疯狂的Cracker | 来源:发表于2016-05-29 19:29 被阅读369次
背景:

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

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

此方法只适用于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直接使用代理方法,但是个人不建议这样使用。

相关文章

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

    背景: 公司项目里要接入全景播放器,但是项目里所有的界面都只支持竖屏,但是播放器技术总监要求我必须能够设置播放器横...

  • iOS旋转屏幕设置总结

    iOS开发中,经常会碰到某些页面需要支持横屏显示,某些又仅支持竖屏显示,那怎么样才能比较完美的实现各个界面的横竖屏...

  • 安卓横竖屏切换的几种情况

    横竖屏切换在APP中的应用非常常见。有的界面只能在横屏或竖屏显示,有的界面可以横竖屏自适应,有的横竖屏使用不同的界...

  • iOS零散知识点总结

    1.设置屏幕常亮 2.竖屏应用中部分界面强制转横屏 AppDelegate 类 某个类需要横竖屏切换,在类中实现 ...

  • JS 与 IOS 交互-横竖屏切换

    IOS 设备横竖屏情况 一般情形 所有界面都支持横竖屏切换如果App的所有切面都要支持横竖屏的切换,那只需要勾选【...

  • iOS 部分界面强制屏幕横屏

    不赘述直接切入本次话题部分界面屏幕横屏 因为是部分界面屏幕横屏故我们先设置项目只能竖屏,如下:在AppDelega...

  • iOS屏幕旋转相关的问题

    最近做项目遇到了屏幕旋转的问题,先说一下我的需求吧:app除了视频播放这一个界面支持横竖屏外,其他界面都只支持竖屏...

  • 视频播放支持横屏

    应用整体只支持竖屏,只有特定的某个界面支持横屏 解决方法: 1.在项目中plist文件中设置支持转屏方向 转屏控制...

  • iOS 视频横竖屏窗口解决方案

    1.其他界面是竖屏,有个界面只支持横屏 #pragma mark 强制横屏的方法 - (BOOL)shouldAu...

  • iOS: 手机 支持屏幕 方向

    [经验]iOS app整体是竖屏(横屏),某个页面却支持横竖屏](http://blog.csdn.net/hhe...

网友评论

    本文标题:iOS界面设置竖屏,部分界面支持横竖屏

    本文链接:https://www.haomeiwen.com/subject/zbtxdttx.html