extension UIViewController{
class func loadMethodSwizzing(){
let originalSelector = #selector(UIViewController.present(_:animated:completion:))
let swizzledSelector = #selector(UIViewController.my_present(_:animated:completion:))
let originalMethod = class_getInstanceMethod(self, originalSelector)
let swizzledMethod = class_getInstanceMethod(self, swizzledSelector)
let didAddMethod: Bool = class_addMethod(self, originalSelector, method_getImplementation(swizzledMethod!), method_getTypeEncoding(swizzledMethod!))
if didAddMethod {
class_replaceMethod(self, swizzledSelector, method_getImplementation(originalMethod!), method_getTypeEncoding(originalMethod!))
} else {
method_exchangeImplementations(originalMethod!, swizzledMethod!)
}
}
@objc func my_present(_ vc:UIViewController,animated: Bool,completion:(()->())?) {
vc.modalPresentationStyle = .fullScreen
self.my_present(vc, animated: animated, completion: completion)
}
}
使用方法:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool{
UIViewController.loadMethodSwizzing()
initWindow()
return true
}
let vc = VideoPlayerController()
self.present(vc, animated: true) {
print("跳转到视频界面")
}
参考了 这篇 文章,感谢作者的分享 点击查看 ,我自己做了一些扩展
|