123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- 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
- }
- }
|