No Description

NavigationControllerProxy.swift 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. //
  2. // NavigationControllerProxy.swift
  3. // PaiaiUIKit
  4. //
  5. // Created by ffib on 2019/1/16.
  6. // Copyright © 2019 yb. All rights reserved.
  7. //
  8. import Foundation
  9. class NavigationControllerProxy: NSObject, UINavigationControllerDelegate {
  10. weak var delegate: NavigationControllerDelegate?
  11. init(target: NavigationControllerDelegate) {
  12. delegate = target
  13. }
  14. func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
  15. delegate?.navigationController(navigationController, willShow: viewController, animated: true)
  16. }
  17. func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
  18. delegate?.navigationController(navigationController, didShow: viewController, animated: true)
  19. }
  20. func navigationController(_ navigationController: UINavigationController,
  21. animationControllerFor operation: UINavigationController.Operation,
  22. from fromVC: UIViewController,
  23. to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
  24. return delegate?.navigationController(navigationController,
  25. animationControllerFor: operation,
  26. from: fromVC, to: toVC)
  27. }
  28. }
  29. extension UINavigationController {
  30. private struct AssociatedKeys {
  31. static var proxyKey = "NavigationControllerProxyKey"
  32. }
  33. private var proxy: NavigationControllerProxy? {
  34. get { return objc_getAssociatedObject(self, &AssociatedKeys.proxyKey) as? NavigationControllerProxy }
  35. set { objc_setAssociatedObject(self, &AssociatedKeys.proxyKey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC) }
  36. }
  37. func setDelegate<T: NavigationControllerDelegate>(_ target: T) {
  38. proxy = NavigationControllerProxy(target: target)
  39. delegate = proxy
  40. }
  41. }