Created
August 4, 2017 08:33
-
-
Save tooszovski/9f3820f9101b7d2b57b83baf9c4a435e to your computer and use it in GitHub Desktop.
swift 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension UINavigationController { | |
open override class func initialize() { | |
if self != UINavigationController.self { return } | |
let swizzlingClosure: () = { | |
UINavigationController().swizzlePushViewController() | |
}() | |
swizzlingClosure | |
} | |
private func swizzlePushViewController() { | |
let originalSelector = #selector(UINavigationController.pushViewController(_:animated:)) | |
let swizzledSelector = #selector(UINavigationController.ai_pushViewController(_:animated:)) | |
let originalMethod = class_getInstanceMethod(UINavigationController.self, originalSelector) | |
let swizzledMethod = class_getInstanceMethod(UINavigationController.self, swizzledSelector) | |
let flag = class_addMethod(UINavigationController.self, | |
originalSelector, | |
method_getImplementation(swizzledMethod), | |
method_getTypeEncoding(swizzledMethod)) | |
if flag { | |
class_replaceMethod(UINavigationController.self, | |
swizzledSelector, | |
method_getImplementation(originalMethod), | |
method_getTypeEncoding(originalMethod)) | |
} else { | |
method_exchangeImplementations(originalMethod, swizzledMethod) | |
} | |
} | |
@objc private func ai_pushViewController(_ viewController: UIViewController, animated: Bool) { | |
print(viewController) | |
self.ai_pushViewController(viewController, animated: animated) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment