Geen omschrijving

NavigationBarProxy.swift 1.1KB

    // // NavigationBarProxy.swift // PaiaiUIKit // // Created by ffib on 2019/1/30. // Copyright © 2019 yb. All rights reserved. // 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 } }