UINavigationBar+Flat.swift 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. //
  2. // UINavigationBar+Flat.swift
  3. // Segmentio
  4. //
  5. // Created by Dmitriy Demchenko
  6. // Copyright © 2016 Yalantis Mobile. All rights reserved.
  7. //
  8. import UIKit
  9. private var flatAssociatedObjectKey: UInt8 = 0
  10. /*
  11. An extension that adds a "flat" field to UINavigationBar. This flag, when
  12. enabled, removes the shadow under the navigation bar.
  13. */
  14. @IBDesignable extension UINavigationBar {
  15. @IBInspectable var flat: Bool {
  16. get {
  17. guard let obj = objc_getAssociatedObject(self, &flatAssociatedObjectKey) as? NSNumber else {
  18. return false
  19. }
  20. return obj.boolValue;
  21. }
  22. set {
  23. if (newValue) {
  24. let void = UIImage()
  25. setBackgroundImage(void, for: .any, barMetrics: .default)
  26. shadowImage = void
  27. } else {
  28. setBackgroundImage(nil, for: .any, barMetrics: .default)
  29. shadowImage = nil
  30. }
  31. objc_setAssociatedObject(self, &flatAssociatedObjectKey, NSNumber(value: newValue as Bool),
  32. objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  33. }
  34. }
  35. }