美文网首页
iOS 强制横屏或强制竖屏

iOS 强制横屏或强制竖屏

作者: 浅宇落 | 来源:发表于2019-10-09 10:07 被阅读0次

灵活设置横竖屏,不用区分Push还是Present,都是可以设置。

第一步

勾选方向.png

AppDelegate.h中添加旋转属性

/** 是否允许转向 */
@property(nonatomic,assign) BOOL allowRotation;

AppDelegate.m中添加转屏的代理方法

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window {
    
    if (self.allowRotation == YES) {
        //横屏
        return UIInterfaceOrientationMaskLandscape;
        
    }else{
        //竖屏
        return UIInterfaceOrientationMaskPortrait;
        
    }
}

第二步

设置横竖屏的核心方法,我是直接把这个方法添加到了UIDevice的分类中,代码如下:

UIDevice+TFDevice.h

#import <UIKit/UIKit.h>
@interface UIDevice (TFDevice)
/**
 * @interfaceOrientation 输入要强制转屏的方向
 */
+ (void)switchNewOrientation:(UIInterfaceOrientation)interfaceOrientation;
@end

UIDevice+TFDevice.m

#import "UIDevice+TFDevice.h"
 
@implementation UIDevice (TFDevice)
 
+ (void)switchNewOrientation:(UIInterfaceOrientation)interfaceOrientation {
        
 NSNumber *resetOrientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];
 [[UIDevice currentDevice] setValue:resetOrientationTarget forKey:@"orientation"];
 NSNumber *orientationTarget = [NSNumber numberWithInt:interfaceOrientation];
 [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
    
}
@end

第三步

在需要设置横屏的控制器的ViewDidLoad中添加下面代码:

- (void)viewDidLoad {
    [super viewDidLoad];
 
    AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    //允许转成横屏
    appDelegate.allowRotation = YES;
    //调用横屏代码
    [UIDevice switchNewOrientation:UIInterfaceOrientationLandscapeRight];

    // 屏幕常亮(不然会息屏)
    [[UIApplication sharedApplication] setIdleTimerDisabled:YES];
}

需要注意的是push过去的时候变成横屏,pop出去的时候在设置竖屏,此时最好禁用系统的侧滑返回手势。

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //禁用侧滑手势方法
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    //禁用侧滑手势方法
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
}

push控制器:

//点击导航栏返回按钮的时候调用,所以Push出的控制器最好禁用侧滑手势:
AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
appDelegate.allowRotation = NO;//关闭横屏仅允许竖屏
//切换到竖屏
[UIDevice switchNewOrientation:UIInterfaceOrientationPortrait];
   
[self.navigationController popViewControllerAnimated:YES];

// 还原
[[UIApplication sharedApplication] setIdleTimerDisabled:NO];

present控制器:

AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
appDelegate.allowRotation = NO;//关闭横屏仅允许竖屏
//切换到竖屏
[UIDevice switchNewOrientation:UIInterfaceOrientationPortrait];
    
[self dismissViewControllerAnimated:YES completion:nil];

// 还原
[[UIApplication sharedApplication] setIdleTimerDisabled:NO];

到此大功告成, 快试试效果吧.

相关文章

  • iOS强制横屏或强制竖屏

    第一种解决方案(不推荐,直接跳过看第二种解决方案): 需求: 强制横竖屏,在某些情况下非常重要,在网上找了好多解决...

  • iOS强制横屏或强制竖屏

    第一种解决方案(不推荐,直接跳过看第二种解决方案): -,需求:强制横竖屏,在某些情况下非常重要,在网上找了好多解...

  • iOS强制横屏或强制竖屏

    原文链接https://my.oschina.net/huqiji/blog/3031940第一种方法会出现无法转...

  • iOS 强制横屏或强制竖屏

    灵活设置横竖屏,不用区分Push还是Present,都是可以设置。 第一步 在AppDelegate.h中添加旋转...

  • iOS强制横屏或强制竖屏

    第一种解决方案(不推荐,直接跳过看第二种解决方案): -,需求:强制横竖屏,在某些情况下非常重要,在网上找了好多解...

  • iOS强制横屏或强制竖屏

    第一种解决方案(不推荐,直接跳过看第二种解决方案): -,需求:强制横竖屏,在某些情况下非常重要,在网上找了好多解...

  • H5页面自动适应横竖屏

    对于样式: 通过html标签可强制移动端浏览器横屏或竖屏但兼容性较差,目前仅有: UC强制竖屏: QQ强制竖屏: ...

  • iOS 强制转屏 强制横屏 强制竖屏

    今天项目中遇到正在看视频的时候账号被挤,如果当时是横屏的情况下,需要强制竖屏。真头疼,网上找了好多方法,终于解决啦...

  • iOS 屏幕旋转控制

    /** 屏幕旋转控制 allowRotateType ==(0强制竖屏,1横竖屏,2~强制横屏 )*/ (UIIn...

  • iOS强制转屏

    引言 遇到需要转屏的功能,查资料刚好看到一篇iOS强制横屏或强制竖屏,不过发现里面的方法有点太不可取,做了下修改。...

网友评论

      本文标题:iOS 强制横屏或强制竖屏

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