适配iOS13 全屏显示、暗黑模式
2020-07-08 本文已影响0人
KevinChein
//
// 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