如何在 swift 4 中调整 UIViewController 中的 viewDidLoad 方法? swift 4 中弃用了 initialize()
How to swizzle viewDidLoad method in UIViewController in swift 4? initialize() is deprecated in swift 4
我想在 UIViewController
扩展中调整 viewDidLoad
,但是 initialize()
方法在 swift3
中被覆盖并且正在工作,但 swift4
不支持.谁能给我任何解决方案?我在 swift3
中的代码如下。
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)
}
}
并且有一个全局方法
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)
}
}
我在 AppDelegate
中添加了以下代码,而不是 swizzling
let barButtonItemAppearance = UIBarButtonItem.appearance()
barButtonItemAppearance.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.clear], for: .normal)
barButtonItemAppearance.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.clear], for: .highlighted)
我想在 UIViewController
扩展中调整 viewDidLoad
,但是 initialize()
方法在 swift3
中被覆盖并且正在工作,但 swift4
不支持.谁能给我任何解决方案?我在 swift3
中的代码如下。
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)
}
}
并且有一个全局方法
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)
}
}
我在 AppDelegate
中添加了以下代码,而不是 swizzling
let barButtonItemAppearance = UIBarButtonItem.appearance()
barButtonItemAppearance.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.clear], for: .normal)
barButtonItemAppearance.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.clear], for: .highlighted)