美文网首页OC-开发案例收集
iOS 强制横屏(Push和模态)

iOS 强制横屏(Push和模态)

作者: 啸狼天 | 来源:发表于2019-05-21 14:29 被阅读0次

# iOS 强制横屏(Push和模态)

iOS开发过程中,有时候需要页面强制横屏。

下面这种方法是不管手机有没有开启横竖屏锁定都会强制横屏

Snip20190521_3.png
  • AppDelegate.h声明一个全局变量

@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property(nonatomic,assign) BOOL allowRotation;//是否允许转向(即横屏)
@end

  • AppDelegate.m中实现
#pragma mark -
#pragma mark -
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
    if (_allowRotation == YES) {
        return UIInterfaceOrientationMaskLandscapeLeft;
    }else{
        return (UIInterfaceOrientationMaskPortrait);
    }
}
  • 新建一个UIViewController的Category

@interface UIViewController (Util)

/**
 强制横屏方法
 横屏(如果属性值为YES,仅允许屏幕向左旋转,否则仅允许竖屏)
 @param fullscreen 屏幕方向
 */
- (void)setNewOrientation:(BOOL)fullscreen;


@end
#import "UIViewController+Util.h"
#import <objc/runtime.h>
#import "AppDelegate.h"

#define AtAppDelegate ((AppDelegate *)[UIApplication sharedApplication].delegate)

//定义常量 必须是C语言字符串
static char *FullScreenAllowRotationKey = "FullScreenAllowRotationKey";

@implementation UIViewController (Util)

void swizzleMethod(Class class, SEL originalSelector, SEL swizzledSelector){
    // the method might not exist in the class, but in its superclass
    Method originalMethod = class_getInstanceMethod(class, originalSelector);
    Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
    
    // class_addMethod will fail if original method already exists
    BOOL didAddMethod = class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
    
    // the method doesn’t exist and we just added one
    if (didAddMethod) {
        class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
    }else {
        method_exchangeImplementations(originalMethod, swizzledMethod);
    }
}


/**
 是否允许横屏
 
 @return bool YES 允许 NO 不允许
 */
- (BOOL)shouldAutorotate1{
    BOOL flag = objc_getAssociatedObject(self, FullScreenAllowRotationKey);
    return flag;
}


/**
 屏幕方向
 
 @return 屏幕方向
 */
- (UIInterfaceOrientationMask)supportedInterfaceOrientations1{
    //get方法通过key获取对象
    BOOL flag = objc_getAssociatedObject(self, FullScreenAllowRotationKey);
    if (flag) {
        return UIInterfaceOrientationMaskLandscapeLeft;
    }else{
        return UIInterfaceOrientationMaskPortrait;
    }
}


- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation1{
    BOOL flag = objc_getAssociatedObject(self, FullScreenAllowRotationKey);
    if (flag) {
        return UIInterfaceOrientationLandscapeLeft;
    }else{
        return UIInterfaceOrientationPortrait;
    }
}

/**
 强制横屏方法
 
 @param fullscreen 屏幕方向
 */
- (void)setNewOrientation:(BOOL)fullscreen{
    AtAppDelegate.allowRotation = fullscreen;
     objc_setAssociatedObject(self, FullScreenAllowRotationKey,[NSNumber numberWithBool:fullscreen], OBJC_ASSOCIATION_ASSIGN);
    
    swizzleMethod([self class], @selector(shouldAutorotate), @selector(shouldAutorotate1));
    swizzleMethod([self class], @selector(supportedInterfaceOrientations), @selector(supportedInterfaceOrientations1));
    swizzleMethod([self class], @selector(preferredInterfaceOrientationForPresentation), @selector(preferredInterfaceOrientationForPresentation1));
    
    @autoreleasepool {
        if (fullscreen) {
            NSNumber *resetOrientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];
            [[UIDevice currentDevice] setValue:resetOrientationTarget forKey:@"orientation"];
            NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
            [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
        }else{
            NSNumber *resetOrientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];
            [[UIDevice currentDevice] setValue:resetOrientationTarget forKey:@"orientation"];
            NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
            [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
        }
    }
}

新建一个UIViewController(需要横屏的控制器)

#import "LandscapeViewController.h"
#import "AppDelegate.h"

@interface LandscapeViewController ()

@end

@implementation LandscapeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.view.backgroundColor = [UIColor redColor];

}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
   [self setNewOrientation:YES];
    
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];    
    [self setNewOrientation:NO];
}


@end

这样强制横屏就做好了

相关文章

  • iOS 强制横屏(Push和模态)

    # iOS 强制横屏(Push和模态) iOS开发过程中,有时候需要页面强制横屏。 下面这种方法是不管手机有没有开...

  • 横屏竖屏切换

    项目只有竖屏的时侯,在某个页面强制横屏模态的形式推入: 在此页面进行强制转屏 转屏页面 push:推入此类继承UI...

  • iOS push横屏小记(强制横屏)

    present 横屏逻辑很简单,但是有一个问题,在present新VC时,原VC导航栏会向上抖动,看起来效...

  • 横竖屏

    需求: 让push的ViewController界面强制横屏 一、配置 二、添加强制横屏方法 pragma mar...

  • iOS强制横屏

    iOS强制横屏

  • iOS 强制横屏和竖屏

    感谢这位同学的文章 需求:只有一个页面横屏而且是强制横屏,其他页面竖屏而且强制竖屏。 说明:本文方法只适用于pre...

  • iOS强制横屏

    朋友给的场景,一个vc present一个nav包着一个vc,这个被present出来的vc要求横屏,dismi...

  • iOS强制横屏

    前言 最近在项目中有一个需求,就是进入某一个页面时要强制横屏,其他界面不做限制,查了资料发现有些方法对我的需求只有...

  • iOS 强制横屏

    NSNumber *orienTarget=[NSNumber numberWithInt: UIInterfac...

  • iOS强制横屏

    项目有一个需求:项目中某一个视图控制器需要强制横屏(或竖屏),并且不能转屏;其他的控制器横竖屏都可以。查了很多资料...

网友评论

    本文标题:iOS 强制横屏(Push和模态)

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