我想在UIViewController
扩展中viewDidLoad
切换,但swift4
不支持initialize()
swift3
中被覆盖并且正在工作的方法。谁能给我任何解决方案?我在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
中添加了以下代码,如果旋转
let barButtonItemAppearance = UIBarButtonItem.appearance()
barButtonItemAppearance.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.clear], for: .normal)
barButtonItemAppearance.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.clear], for: .highlighted)