如何在 swift 4 中的 UIViewController 中切换 viewDidLoad 方法?initializ



我想在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)

相关内容

  • 没有找到相关文章

最新更新