暫無描述

NavigationControllerProxy.swift 2.2KB

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