美文网首页
适配iOS13 全屏显示、暗黑模式

适配iOS13 全屏显示、暗黑模式

作者: KevinChein | 来源:发表于2020-07-08 18:15 被阅读0次
//
//  UIViewController+AdjustiOS13Controller.m
//  AdjustiOS13_Demo
//
//  Created by KevinChien on 2019/11/13.
//  Copyright © 2019 qianjinwu. All rights reserved.
//

#import "UIViewController+AdjustiOS13Controller.h"
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wobjc-protocol-method-implementation"
#import <objc/message.h>
#import <objc/runtime.h>

@implementation UIViewController (AdjustiOS13Controller)
// 命名一定要规范,尽量戴上自己的前缀,否则容易跟其他框架的命名造成冲突
void Kevin_SwizzleFunc(Class baseClass, SEL originalSel,SEL destinationSel){

    Method originalMethod = class_getInstanceMethod(baseClass, originalSel);
    Method destinationMethod = class_getInstanceMethod(baseClass, destinationSel);

    IMP originalMethodIMP = method_getImplementation(originalMethod);
    IMP destinationMethodIMP = method_getImplementation(destinationMethod);
    // 如果被其他 hook 框架交换过了就不在进行交换--防止交换错乱
    if (_objc_msgForward == originalMethodIMP || _objc_msgForward == destinationMethodIMP) {
        return;
    }

    BOOL didAddMethod = class_addMethod(baseClass, originalSel, destinationMethodIMP, method_getTypeEncoding(destinationMethod));
    if (didAddMethod) {
        class_replaceMethod(baseClass, destinationSel, originalMethodIMP, method_getTypeEncoding(originalMethod));
    }else{
        method_exchangeImplementations(originalMethod, destinationMethod);
    }
    return;
}

+(void)load{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Kevin_SwizzleFunc(UIViewController.class, @selector(presentViewController:animated:completion:), @selector(kevin_presentViewController:animated:completion:));
    });
}
- (void)kevin_presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion {
    if (@available(iOS 13.0, *)) {
        viewControllerToPresent.modalPresentationStyle = UIModalPresentationFullScreen;
        viewControllerToPresent.overrideUserInterfaceStyle = UIUserInterfaceStyleLight;
    } else {
    // Fallback on earlier versions
    }
    //call real viewDidLoad
    [self kevin_presentViewController:viewControllerToPresent animated:flag completion:completion];
}

@end

相关文章

网友评论

      本文标题:适配iOS13 全屏显示、暗黑模式

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