Нет описания

NavigationBarProxy.swift 1.1KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. //
  2. // NavigationBarProxy.swift
  3. // PaiaiUIKit
  4. //
  5. // Created by ffib on 2019/1/30.
  6. // Copyright © 2019 yb. All rights reserved.
  7. //
  8. import UIKit
  9. class NavigationBarProxy: NSObject, UINavigationBarDelegate {
  10. weak var delegate: NavigationBarDelegate?
  11. init(target: NavigationBarDelegate) {
  12. delegate = target
  13. }
  14. func navigationBar(_ navigationBar: UINavigationBar, shouldPush item: UINavigationItem) -> Bool {
  15. guard let d = delegate else { return true }
  16. return d.navigationBar(navigationBar, shouldPush: item)
  17. }
  18. }
  19. extension UINavigationBar {
  20. private struct AssociatedKeys {
  21. static var proxyKey = "NavigationBarProxyKey"
  22. }
  23. private var proxy: NavigationBarProxy? {
  24. get { return objc_getAssociatedObject(self, &AssociatedKeys.proxyKey) as? NavigationBarProxy }
  25. set { objc_setAssociatedObject(self, &AssociatedKeys.proxyKey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC) }
  26. }
  27. func setDelegate<T: NavigationBarDelegate>(_ target: T) {
  28. proxy = NavigationBarProxy(target: target)
  29. delegate = proxy
  30. }
  31. }