EmptyDataSource.swift 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. //
  2. // EmptyDataSource.swift
  3. // EmptyKit
  4. //
  5. // Created by archerzz on 16/11/15.
  6. // Copyright © 2016年 archerzz. All rights reserved.
  7. //
  8. import Foundation
  9. import UIKit
  10. /// EmptyDataSource
  11. public protocol EmptyDataSource: class {
  12. /**
  13. Ask the datasource for the sections you want to ignore. Default is nil.
  14. - author: archerzz
  15. - date: 2016-11-23
  16. - parameter view: Empty view's superview.
  17. - returns: The sections you want to ignore.
  18. */
  19. func sectionsToIgnore(in view: UIView) -> [Int]?
  20. /**
  21. Ask the datasource for an image you want to display. Default is nil.
  22. - author: archerzz
  23. - date: 2016-11-23
  24. - parameter view: Empty view's superview.
  25. - returns: An image you want to display.
  26. */
  27. func imageForEmpty(in view: UIView) -> UIImage?
  28. /**
  29. Ask the datasource for a tint color that you want to rendering the image. Default is nil, and the redering mode of image is alwaysOriginals.
  30. If you specific a color, the redering mode of image is alwaysTemplate.
  31. - author: archerzz
  32. - date: 2016-11-23
  33. - parameter view: Empty view's superview.
  34. - returns: A tint color that you want to rendering the image.
  35. */
  36. func imageTintColorForEmpty(in view: UIView) -> UIColor?
  37. /**
  38. Ask the datasource for an animation you want to add to image's layer. Default is nil.
  39. - author: archerzz
  40. - date: 2016-11-23
  41. - parameter view: Empty view's superview.
  42. - returns: An animation you want to add to image's layer.
  43. */
  44. func imageAnimationForEmpty(in view: UIView) -> CAAnimation?
  45. /**
  46. Ask the datasource for a title you want to display. Default is nil.
  47. - author: archerzz
  48. - date: 2016-11-23
  49. - parameter view: Empty view's superview.
  50. - returns: A title you want to display.
  51. */
  52. func titleForEmpty(in view: UIView) -> NSAttributedString?
  53. /**
  54. Ask the datasource for a description you want to display. Default is nil.
  55. - author: archerzz
  56. - date: 2016-11-23
  57. - parameter view: Empty view's superview.
  58. - returns: A description you want to display.
  59. */
  60. func descriptionForEmpty(in view: UIView) -> NSAttributedString?
  61. /**
  62. Ask the datasource for a vertical offset you want to set up to align the content. Default is 0.
  63. - author: archerzz
  64. - date: 2016-11-23
  65. - parameter view: Empty view's superview.
  66. - returns: A vertical offset you want to set up to align the content.
  67. */
  68. func verticalOffsetForEmpty(in view: UIView) -> CGFloat
  69. /**
  70. Ask the datasource for a vertical space between elements. Default is 10 pts.
  71. - author: archerzz
  72. - date: 2016-11-23
  73. - parameter view: Empty view's superview.
  74. - returns: A vertical space.
  75. */
  76. func verticalSpaceForEmpty(in view: UIView) -> CGFloat
  77. /**
  78. Ask the datasource for a horizontal space for the leading and trailing. Default is 20 pts.
  79. - author: archerzz
  80. - date: 2016-11-23
  81. - parameter view: Empty view's superview
  82. - returns: A horizontal space for the leading and trailing
  83. */
  84. func horizontalSpaceForEmpty(in view: UIView) -> CGFloat
  85. /**
  86. Ask the datasource for a title you want to display in the different state of button. Default is nil
  87. - author: archerzz
  88. - date: 2016-11-23
  89. - parameter view: Empty view's superview
  90. - returns: A title you want to display in the different state of button
  91. */
  92. func buttonTitleForEmpty(forState state: UIControl.State, in view: UIView) -> NSAttributedString?
  93. /**
  94. Ask the datasource for a image you want to display in the different state of button. Default is nil
  95. - author: archerzz
  96. - date: 2016-11-23
  97. - parameter view: Empty view's superview
  98. - returns: A image you want to display in the different state of button
  99. */
  100. func buttonImageForEmpty(forState state: UIControl.State, in view: UIView) -> UIImage?
  101. /**
  102. Ask the datasource for a background image you want to display in the different state of button. Default is nil
  103. - author: archerzz
  104. - date: 2016-11-23
  105. - parameter view: Empty view's superview
  106. - returns: A background image you want to display in the different state of button
  107. */
  108. func buttonBackgroundImageForEmpty(forState state: UIControl.State, in view: UIView) -> UIImage?
  109. /**
  110. Ask the datasource for a background color you want to display in the different state of button. Default is clear
  111. - author: archerzz
  112. - date: 2016-11-23
  113. - parameter view: Empty view's superview
  114. - returns: A background color you want to display in the different state of button
  115. */
  116. func buttonBackgroundColorForEmpty(in view: UIView) -> UIColor
  117. /**
  118. Ask the datasource for a minimum width of the button. Default is 120 pts.
  119. - author: archerzz
  120. - date: 2016-11-23
  121. - parameter view: Empty view's superview.
  122. - returns: A minimum width of the button.
  123. */
  124. func buttonMinimumWidthForEmpty(in view: UIView) -> CGFloat?
  125. /**
  126. Ask the datasource for a background color of the empty view. Default is clear.
  127. - author: archerzz
  128. - date: 2016-11-23
  129. - parameter view: Empty view's superview.
  130. - returns: A background color of the empty view.
  131. */
  132. func backgroundColorForEmpty(in view: UIView) -> UIColor
  133. /**
  134. Ask the datasource for a custom view you want to display in empty view.. Default is nil
  135. - author: archerzz
  136. - date: 2016-11-23
  137. - parameter view: Empty view's superview
  138. - returns: A custom view you want to display in empty view.
  139. */
  140. func customViewForEmpty(in view: UIView) -> UIView?
  141. }
  142. // MARK: - Default imp
  143. public extension EmptyDataSource {
  144. func sectionsToIgnore(in view: UIView) -> [Int]? {
  145. return nil
  146. }
  147. func imageForEmpty(in view: UIView) -> UIImage? {
  148. return nil
  149. }
  150. func imageTintColorForEmpty(in view: UIView) -> UIColor? {
  151. return nil
  152. }
  153. func imageAnimationForEmpty(in view: UIView) -> CAAnimation? {
  154. return nil
  155. }
  156. func titleForEmpty(in view: UIView) -> NSAttributedString? {
  157. return nil
  158. }
  159. func descriptionForEmpty(in view: UIView) -> NSAttributedString? {
  160. return nil
  161. }
  162. func verticalOffsetForEmpty(in view: UIView) -> CGFloat {
  163. return 0
  164. }
  165. func verticalSpaceForEmpty(in view: UIView) -> CGFloat {
  166. return 10
  167. }
  168. func horizontalSpaceForEmpty(in view: UIView) -> CGFloat {
  169. return 20
  170. }
  171. func buttonTitleForEmpty(forState state: UIControl.State, in view: UIView) -> NSAttributedString? {
  172. return nil
  173. }
  174. func buttonImageForEmpty(forState state: UIControl.State, in view: UIView) -> UIImage? {
  175. return nil
  176. }
  177. func buttonBackgroundImageForEmpty(forState state: UIControl.State, in view: UIView) -> UIImage? {
  178. return nil
  179. }
  180. func buttonBackgroundColorForEmpty(in view: UIView) -> UIColor {
  181. return UIColor.clear
  182. }
  183. func backgroundColorForEmpty(in view: UIView) -> UIColor {
  184. return UIColor.clear
  185. }
  186. func buttonMinimumWidthForEmpty(in view: UIView) -> CGFloat? {
  187. return 120
  188. }
  189. func customViewForEmpty(in view: UIView) -> UIView? {
  190. return nil
  191. }
  192. }
  193. // MARK: - UIViewController
  194. public extension EmptyDataSource where Self: UIViewController {
  195. /**
  196. offset will be -navHeight
  197. ```
  198. if let nav = self.navigationController, !nav.isNavigationBarHidden, nav.navigationBar.isTranslucent {
  199. return -nav.navigationBar.bounds.height
  200. }
  201. return 0
  202. ```
  203. - author: archerzz
  204. - date: 2016-11-23
  205. - parameter view: Empty view's superview
  206. - returns: the opposite number of the navigationbar height if isNavigationBarHidden is false and navigationBar.isTranslucent is true, otherwise return 0.
  207. */
  208. func verticalOffsetForEmpty(in view: UIView) -> CGFloat {
  209. if let nav = self.navigationController, !nav.isNavigationBarHidden, nav.navigationBar.isTranslucent {
  210. return -nav.navigationBar.frame.maxY / 2
  211. }
  212. return 0
  213. }
  214. }
  215. // MARK: - UITableViewController
  216. public extension EmptyDataSource where Self: UITableViewController {
  217. /**
  218. offset will be -(navHeight + tableHeaderViewHeight)
  219. ```
  220. var offset: CGFloat = 0
  221. if let nav = self.navigationController, !nav.isNavigationBarHidden, nav.navigationBar.isTranslucent {
  222. offset -= nav.navigationBar.bounds.height
  223. }
  224. if let tableHeaderView = tableView.tableHeaderView, !tableHeaderView.isHidden {
  225. offset -= tableHeaderView.bounds.height / 2
  226. }
  227. return offset
  228. ```
  229. - author: archerzz
  230. - date: 2016-11-23
  231. - parameter view: Empty view's superview
  232. - returns: the opposite number of the navigationbar height if isNavigationBarHidden is false and navigationBar.isTranslucent is true plus the half of the tableHeaderView's height, otherwise return 0.
  233. */
  234. func verticalOffsetForEmpty(in view: UIView) -> CGFloat {
  235. var offset: CGFloat = 0
  236. if let nav = self.navigationController, !nav.isNavigationBarHidden, nav.navigationBar.isTranslucent {
  237. offset -= nav.navigationBar.frame.maxY / 2
  238. }
  239. if let tableHeaderView = tableView.tableHeaderView, !tableHeaderView.isHidden {
  240. return 0
  241. }
  242. return offset
  243. }
  244. }