gpt4 book ai didi

ios - 如何在 swift 4 中调整 UIViewController 中的 viewDidLoad 方法? Swift 4 中不推荐使用initialize()

转载 作者:行者123 更新时间:2023-11-30 12:04:53 24 4
gpt4 key购买 nike

我想在 UIViewController 扩展中混合 viewDidLoad,但在 swift3 中覆盖了 initialize() 方法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)

关于ios - 如何在 swift 4 中调整 UIViewController 中的 viewDidLoad 方法? Swift 4 中不推荐使用initialize(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46787863/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com