RefreshControlerDesignable.swift 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. //
  2. // Created by phimage on 26/03/2017.
  3. // Copyright © 2017 IBAnimatable. All rights reserved.
  4. //
  5. import UIKit
  6. public protocol RefreshControlDesignable: class {
  7. /**
  8. Component need to display a refresh control
  9. */
  10. var hasRefreshControl: Bool { get set }
  11. /**
  12. The tint color of the refresh control
  13. */
  14. var refreshControlTintColor: UIColor? { get set }
  15. /**
  16. The background color of the refresh control
  17. */
  18. var refreshControlBackgroundColor: UIColor? { get set }
  19. }
  20. public extension RefreshControlDesignable where Self: UITableViewController {
  21. func configureRefreshController() {
  22. guard isViewLoaded else {
  23. return
  24. }
  25. configureRefreshController(hasRefreshControl: hasRefreshControl, refreshControl: &refreshControl)
  26. }
  27. }
  28. public extension RefreshControlDesignable where Self: UITableView {
  29. func configureRefreshController() {
  30. if #available(iOS 10.0, *) {
  31. configureRefreshController(hasRefreshControl: hasRefreshControl, refreshControl: &refreshControl)
  32. } else {
  33. var refreshControl = subviews.first { $0 is UIRefreshControl } as? UIRefreshControl
  34. configureRefreshController(hasRefreshControl: hasRefreshControl, refreshControl: &refreshControl)
  35. }
  36. }
  37. }
  38. fileprivate extension RefreshControlDesignable {
  39. func configureRefreshController(hasRefreshControl: Bool, refreshControl: inout UIRefreshControl?) {
  40. guard hasRefreshControl else {
  41. refreshControl = nil
  42. return
  43. }
  44. if refreshControl == nil {
  45. refreshControl = UIRefreshControl()
  46. }
  47. refreshControl?.tintColor = refreshControlTintColor
  48. refreshControl?.backgroundColor = refreshControlBackgroundColor
  49. }
  50. }