123456789101112131415161718192021222324252627282930313233343536373839 |
- import UIKit
- class NavigationBarProxy: NSObject, UINavigationBarDelegate {
- weak var delegate: NavigationBarDelegate?
- init(target: NavigationBarDelegate) {
- delegate = target
- }
-
- func navigationBar(_ navigationBar: UINavigationBar, shouldPush item: UINavigationItem) -> Bool {
- guard let d = delegate else { return true }
- return d.navigationBar(navigationBar, shouldPush: item)
- }
- }
- extension UINavigationBar {
- private struct AssociatedKeys {
- static var proxyKey = "NavigationBarProxyKey"
- }
-
- private var proxy: NavigationBarProxy? {
- get { return objc_getAssociatedObject(self, &AssociatedKeys.proxyKey) as? NavigationBarProxy }
- set { objc_setAssociatedObject(self, &AssociatedKeys.proxyKey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC) }
- }
-
- func setDelegate<T: NavigationBarDelegate>(_ target: T) {
- proxy = NavigationBarProxy(target: target)
- delegate = proxy
- }
- }
|