I want to swizzle viewDidLoad
in a UIViewController
extension but initialize()
method which was overridden in swift3
and was working is not supported by swift4
. Can anyone give me any solution to this? My code in swift3
was as below.
extension UIViewController {
open override class func initialize() {
guard self === UIViewController.self else { return }
swizzling(self)
}
// MARK: - Method Swizzling
func nsh_viewDidLoad() {
self.nsh_viewDidLoad()
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
}
}
and have a global method
private let swizzling: (UIViewController.Type) -> () = { viewController in
let originalSelector = #selector(viewController.viewDidLoad)
let swizzledSelector = #selector(viewController.nsh_viewDidLoad)
let originalMethod = class_getInstanceMethod(viewController, originalSelector)
let swizzledMethod = class_getInstanceMethod(viewController, swizzledSelector)
let didAddMethod = class_addMethod(viewController, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod))
if didAddMethod {
class_replaceMethod(viewController, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod))
} else {
method_exchangeImplementations(originalMethod, swizzledMethod)
}
}
I added the below code in AppDelegate
instead if swizzling
let barButtonItemAppearance = UIBarButtonItem.appearance()
barButtonItemAppearance.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.clear], for: .normal)
barButtonItemAppearance.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.clear], for: .highlighted)