如何在swift 4中的UIViewController中调用viewDidLoad方法? initialize()在swift 4中已弃用

时间:2017-10-17 10:21:43

标签: ios swift uiviewcontroller swift4 swizzling

我希望在viewDidLoad扩展名中调整UIViewController但在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)
    }
}

并有一个全局方法

{% assign min = 65 %}
{% assign max = 80 %}
{% assign diff = max | minus: min %}
{% assign randomNumber = "now" | date: "%N" | modulo: diff | plus: min %}

1 个答案:

答案 0 :(得分:0)

我在AppDelegate中添加了以下代码,如果是混合

let barButtonItemAppearance = UIBarButtonItem.appearance()
        barButtonItemAppearance.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.clear], for: .normal)
        barButtonItemAppearance.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.clear], for: .highlighted)