123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- import Foundation
- class NavigationControllerProxy: NSObject, UINavigationControllerDelegate {
- weak var delegate: NavigationControllerDelegate?
-
- init(target: NavigationControllerDelegate) {
- delegate = target
- }
-
- func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
- delegate?.navigationController(navigationController, willShow: viewController, animated: true)
- }
-
- func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
- delegate?.navigationController(navigationController, didShow: viewController, animated: true)
- }
-
- func navigationController(_ navigationController: UINavigationController,
- animationControllerFor operation: UINavigationController.Operation,
- from fromVC: UIViewController,
- to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
- return delegate?.navigationController(navigationController,
- animationControllerFor: operation,
- from: fromVC, to: toVC)
- }
- }
- extension UINavigationController {
- private struct AssociatedKeys {
- static var proxyKey = "NavigationControllerProxyKey"
- }
-
- private var proxy: NavigationControllerProxy? {
- get { return objc_getAssociatedObject(self, &AssociatedKeys.proxyKey) as? NavigationControllerProxy }
- set { objc_setAssociatedObject(self, &AssociatedKeys.proxyKey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC) }
- }
-
- func setDelegate<T: NavigationControllerDelegate>(_ target: T) {
- proxy = NavigationControllerProxy(target: target)
- delegate = proxy
- }
- }
|