Nav apraksta

NavigationBar.swift 3.0KB

    // // NavigationBar.swift // PaiaiUIKit // // Created by FFIB on 2019/4/23. // Copyright © 2019 FFIB. All rights reserved. // import UIKit class NavigationBar: UINavigationBar { override init(frame: CGRect) { super.init(frame: frame) } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } func getNavigationBarBounds() -> CGRect { let statusHeight = UIApplication.shared.statusBarFrame.height return CGRect(x: 0, y: -statusHeight, width: bounds.width, height: bounds.height + statusHeight) } override func value(forUndefinedKey key: String) -> Any? { return nil } } public extension UINavigationBar { func getBarBackground() -> UIView? { return value(forKeyPath: "_backgroundView") as? UIView } func getBarContentView() -> UIView? { for val in subviews { if let contentClass = NSClassFromString("_UINavigationBarContentView"), val.isKind(of: contentClass) { return val } } return nil } func getShadowView() -> UIView? { guard let barBackground = getBarBackground() else { return nil } for view in barBackground.subviews where view.bounds.height == 0.5 { return view } return nil } internal func apply(for configure: NavigationBarConfiguration) { isHidden = configure.isHidden isTranslucent = configure.isTranslucent barStyle = configure.barStyle shadowImage = configure.shadowImage setBackgroundImage(configure.backgroundImage, for: .default) } internal convenience init(configure: NavigationBarConfiguration) { self.init() apply(for: configure) } } public extension UIToolbar { func getBarBackground() -> UIView? { return value(forKeyPath: "_backgroundView") as? UIView } func getShadowView() -> UIView? { guard let barBackground = getBarBackground() else { return nil } for view in barBackground.subviews where view.bounds.height == 0.5 { return view } return nil } internal convenience init(configure: NavigationBarConfiguration) { self.init() isHidden = configure.isHidden isTranslucent = configure.isTranslucent barStyle = configure.barStyle setShadowImage(configure.shadowImage, forToolbarPosition: .any) setBackgroundImage(configure.backgroundImage, forToolbarPosition: .any, barMetrics: .default) } } extension UIViewController { internal func getFakeBarFrame(for navigationBar: UINavigationBar) -> CGRect? { guard let backgroundView = navigationBar.getBarBackground(), var frame = backgroundView.superview?.convert(backgroundView.frame, to: self.view) else { return nil } frame.origin.x = self.view.bounds.origin.x return frame } }