यहां बताया गया है कि हमने किस तरह से स्विज़लिंग का उपयोग करके यह हासिल किया:
उद्देश्य सी
UIViewController + iOS13Fixes.h
#import <Foundation/Foundation.h>
@interface UIViewController (iOS13Fixes)
@end
UIViewController + iOS13Fixes.m
#import <objc/runtime.h>
@implementation UIViewController (iOS13Fixes)
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Class class = [self class];
SEL originalSelector = @selector(presentViewController:animated:completion:);
SEL swizzledSelector = @selector(swizzled_presentViewController:animated:completion:);
Method originalMethod = class_getInstanceMethod(class, originalSelector);
Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
BOOL methodExists = !class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
if (methodExists) {
method_exchangeImplementations(originalMethod, swizzledMethod);
} else {
class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
}
});
}
- (void)swizzled_presentViewController:(nonnull UIViewController *)viewController animated:(BOOL)animated completion:(void (^)())completion {
if (@available(iOS 13.0, *)) {
if (viewController.modalPresentationStyle == UIModalPresentationAutomatic || viewController.modalPresentationStyle == UIModalPresentationPageSheet) {
viewController.modalPresentationStyle = UIModalPresentationFullScreen;
}
}
[self swizzled_presentViewController:viewController animated:animated completion:completion];
}
@end
तीव्र
UIViewController + iOS13Fixes.swift
import UIKit
@objc public extension UIViewController {
private func swizzled_present(_ viewControllerToPresent: UIViewController, animated: Bool, completion: (() -> Void)?) {
if #available(iOS 13.0, *) {
if viewControllerToPresent.modalPresentationStyle == .automatic || viewControllerToPresent.modalPresentationStyle == .pageSheet {
viewControllerToPresent.modalPresentationStyle = .fullScreen
}
}
self.swizzled_present(viewControllerToPresent, animated: animated, completion: completion)
}
@nonobjc private static let _swizzlePresentationStyle: Void = {
let instance: UIViewController = UIViewController()
let aClass: AnyClass! = object_getClass(instance)
let originalSelector = #selector(UIViewController.present(_:animated:completion:))
let swizzledSelector = #selector(UIViewController.swizzled_present(_:animated:completion:))
let originalMethod = class_getInstanceMethod(aClass, originalSelector)
let swizzledMethod = class_getInstanceMethod(aClass, swizzledSelector)
if let originalMethod = originalMethod, let swizzledMethod = swizzledMethod {
if !class_addMethod(aClass, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod)) {
method_exchangeImplementations(originalMethod, swizzledMethod)
} else {
class_replaceMethod(aClass, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
}
}
}()
@objc static func swizzlePresentationStyle() {
_ = self._swizzlePresentationStyle
}
}
और AppDelegate
, application:didFinishLaunchingWithOptions:
कॉल करके स्विज़लिंग को आमंत्रित करें (केवल स्विफ्ट संस्करण):
UIViewController.swizzlePresentationStyle()
सुनिश्चित करें कि इसे केवल एक बार (उपयोग dispatch_once
या कुछ समकक्ष) कहा जाता है ।
अधिक विधि यहाँ swizzling पर: