OC-开发案例收集

iOS 强制横屏(Push和模态)

2019-05-21  本文已影响0人  啸狼天

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

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

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

Snip20190521_3.png

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

#pragma mark -
#pragma mark -
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
    if (_allowRotation == YES) {
        return UIInterfaceOrientationMaskLandscapeLeft;
    }else{
        return (UIInterfaceOrientationMaskPortrait);
    }
}

@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

这样强制横屏就做好了

上一篇下一篇

猜你喜欢

热点阅读