No Description

NavigationBar.swift 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //
  2. // NavigationBar.swift
  3. // PaiaiUIKit
  4. //
  5. // Created by FFIB on 2019/4/23.
  6. // Copyright © 2019 FFIB. All rights reserved.
  7. //
  8. import UIKit
  9. class NavigationBar: UINavigationBar {
  10. override init(frame: CGRect) {
  11. super.init(frame: frame)
  12. }
  13. required init?(coder aDecoder: NSCoder) {
  14. fatalError("init(coder:) has not been implemented")
  15. }
  16. func getNavigationBarBounds() -> CGRect {
  17. let statusHeight = UIApplication.shared.statusBarFrame.height
  18. return CGRect(x: 0, y: -statusHeight, width: bounds.width, height: bounds.height + statusHeight)
  19. }
  20. override func value(forUndefinedKey key: String) -> Any? {
  21. return nil
  22. }
  23. }
  24. public extension UINavigationBar {
  25. func getBarBackground() -> UIView? {
  26. return value(forKeyPath: "_backgroundView") as? UIView
  27. }
  28. func getBarContentView() -> UIView? {
  29. for val in subviews {
  30. if let contentClass = NSClassFromString("_UINavigationBarContentView"),
  31. val.isKind(of: contentClass) {
  32. return val
  33. }
  34. }
  35. return nil
  36. }
  37. func getShadowView() -> UIView? {
  38. guard let barBackground = getBarBackground() else { return nil }
  39. for view in barBackground.subviews where view.bounds.height == 0.5 {
  40. return view
  41. }
  42. return nil
  43. }
  44. internal func apply(for configure: NavigationBarConfiguration) {
  45. isHidden = configure.isHidden
  46. isTranslucent = configure.isTranslucent
  47. barStyle = configure.barStyle
  48. shadowImage = configure.shadowImage
  49. setBackgroundImage(configure.backgroundImage, for: .default)
  50. }
  51. internal convenience init(configure: NavigationBarConfiguration) {
  52. self.init()
  53. apply(for: configure)
  54. }
  55. }
  56. public extension UIToolbar {
  57. func getBarBackground() -> UIView? {
  58. return value(forKeyPath: "_backgroundView") as? UIView
  59. }
  60. func getShadowView() -> UIView? {
  61. guard let barBackground = getBarBackground() else { return nil }
  62. for view in barBackground.subviews where view.bounds.height == 0.5 {
  63. return view
  64. }
  65. return nil
  66. }
  67. internal convenience init(configure: NavigationBarConfiguration) {
  68. self.init()
  69. isHidden = configure.isHidden
  70. isTranslucent = configure.isTranslucent
  71. barStyle = configure.barStyle
  72. setShadowImage(configure.shadowImage, forToolbarPosition: .any)
  73. setBackgroundImage(configure.backgroundImage, forToolbarPosition: .any, barMetrics: .default)
  74. }
  75. }
  76. extension UIViewController {
  77. internal func getFakeBarFrame(for navigationBar: UINavigationBar) -> CGRect? {
  78. guard let backgroundView = navigationBar.getBarBackground(),
  79. var frame = backgroundView.superview?.convert(backgroundView.frame, to: self.view)
  80. else { return nil }
  81. frame.origin.x = self.view.bounds.origin.x
  82. return frame
  83. }
  84. }