UIScrollView+EmptyDataSet.m 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077
  1. //
  2. // UIScrollView+EmptyDataSet.m
  3. // DZNEmptyDataSet
  4. // https://github.com/dzenbot/DZNEmptyDataSet
  5. //
  6. // Created by Ignacio Romero Zurbuchen on 6/20/14.
  7. // Copyright (c) 2016 DZN Labs. All rights reserved.
  8. // Licence: MIT-Licence
  9. //
  10. #import "UIScrollView+EmptyDataSet.h"
  11. #import <objc/runtime.h>
  12. @interface UIView (DZNConstraintBasedLayoutExtensions)
  13. - (NSLayoutConstraint *)equallyRelatedConstraintWithView:(UIView *)view attribute:(NSLayoutAttribute)attribute;
  14. @end
  15. @interface DZNWeakObjectContainer : NSObject
  16. @property (nonatomic, readonly, weak) id weakObject;
  17. - (instancetype)initWithWeakObject:(id)object;
  18. @end
  19. @interface DZNEmptyDataSetView : UIView
  20. @property (nonatomic, readonly) UIView *contentView;
  21. @property (nonatomic, readonly) UILabel *titleLabel;
  22. @property (nonatomic, readonly) UILabel *detailLabel;
  23. @property (nonatomic, readonly) UIImageView *imageView;
  24. @property (nonatomic, readonly) UIButton *button;
  25. @property (nonatomic, strong) UIView *customView;
  26. @property (nonatomic, strong) UITapGestureRecognizer *tapGesture;
  27. @property (nonatomic, assign) CGFloat verticalOffset;
  28. @property (nonatomic, assign) CGFloat verticalSpace;
  29. @property (nonatomic, assign) BOOL fadeInOnDisplay;
  30. - (void)setupConstraints;
  31. - (void)prepareForReuse;
  32. @end
  33. #pragma mark - UIScrollView+EmptyDataSet
  34. static char const * const kEmptyDataSetSource = "emptyDataSetSource";
  35. static char const * const kEmptyDataSetDelegate = "emptyDataSetDelegate";
  36. static char const * const kEmptyDataSetView = "emptyDataSetView";
  37. #define kEmptyImageViewAnimationKey @"com.dzn.emptyDataSet.imageViewAnimation"
  38. @interface UIScrollView () <UIGestureRecognizerDelegate>
  39. @property (nonatomic, readonly) DZNEmptyDataSetView *emptyDataSetView;
  40. @end
  41. @implementation UIScrollView (DZNEmptyDataSet)
  42. #pragma mark - Getters (Public)
  43. - (id<DZNEmptyDataSetSource>)emptyDataSetSource
  44. {
  45. DZNWeakObjectContainer *container = objc_getAssociatedObject(self, kEmptyDataSetSource);
  46. return container.weakObject;
  47. }
  48. - (id<DZNEmptyDataSetDelegate>)emptyDataSetDelegate
  49. {
  50. DZNWeakObjectContainer *container = objc_getAssociatedObject(self, kEmptyDataSetDelegate);
  51. return container.weakObject;
  52. }
  53. - (BOOL)isEmptyDataSetVisible
  54. {
  55. UIView *view = objc_getAssociatedObject(self, kEmptyDataSetView);
  56. return view ? !view.hidden : NO;
  57. }
  58. #pragma mark - Getters (Private)
  59. - (DZNEmptyDataSetView *)emptyDataSetView
  60. {
  61. DZNEmptyDataSetView *view = objc_getAssociatedObject(self, kEmptyDataSetView);
  62. if (!view)
  63. {
  64. view = [DZNEmptyDataSetView new];
  65. view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
  66. view.hidden = YES;
  67. view.tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dzn_didTapContentView:)];
  68. view.tapGesture.delegate = self;
  69. [view addGestureRecognizer:view.tapGesture];
  70. [self setEmptyDataSetView:view];
  71. }
  72. return view;
  73. }
  74. - (BOOL)dzn_canDisplay
  75. {
  76. if (self.emptyDataSetSource && [self.emptyDataSetSource conformsToProtocol:@protocol(DZNEmptyDataSetSource)]) {
  77. if ([self isKindOfClass:[UITableView class]] || [self isKindOfClass:[UICollectionView class]] || [self isKindOfClass:[UIScrollView class]]) {
  78. return YES;
  79. }
  80. }
  81. return NO;
  82. }
  83. - (NSInteger)dzn_itemsCount
  84. {
  85. NSInteger items = 0;
  86. // UIScollView doesn't respond to 'dataSource' so let's exit
  87. if (![self respondsToSelector:@selector(dataSource)]) {
  88. return items;
  89. }
  90. // UITableView support
  91. if ([self isKindOfClass:[UITableView class]]) {
  92. UITableView *tableView = (UITableView *)self;
  93. id <UITableViewDataSource> dataSource = tableView.dataSource;
  94. NSInteger sections = 1;
  95. if (dataSource && [dataSource respondsToSelector:@selector(numberOfSectionsInTableView:)]) {
  96. sections = [dataSource numberOfSectionsInTableView:tableView];
  97. }
  98. if (dataSource && [dataSource respondsToSelector:@selector(tableView:numberOfRowsInSection:)]) {
  99. for (NSInteger section = 0; section < sections; section++) {
  100. items += [dataSource tableView:tableView numberOfRowsInSection:section];
  101. }
  102. }
  103. }
  104. // UICollectionView support
  105. else if ([self isKindOfClass:[UICollectionView class]]) {
  106. UICollectionView *collectionView = (UICollectionView *)self;
  107. id <UICollectionViewDataSource> dataSource = collectionView.dataSource;
  108. NSInteger sections = 1;
  109. if (dataSource && [dataSource respondsToSelector:@selector(numberOfSectionsInCollectionView:)]) {
  110. sections = [dataSource numberOfSectionsInCollectionView:collectionView];
  111. }
  112. if (dataSource && [dataSource respondsToSelector:@selector(collectionView:numberOfItemsInSection:)]) {
  113. for (NSInteger section = 0; section < sections; section++) {
  114. items += [dataSource collectionView:collectionView numberOfItemsInSection:section];
  115. }
  116. }
  117. }
  118. return items;
  119. }
  120. #pragma mark - Data Source Getters
  121. - (NSAttributedString *)dzn_titleLabelString
  122. {
  123. if (self.emptyDataSetSource && [self.emptyDataSetSource respondsToSelector:@selector(titleForEmptyDataSet:)]) {
  124. NSAttributedString *string = [self.emptyDataSetSource titleForEmptyDataSet:self];
  125. if (string) NSAssert([string isKindOfClass:[NSAttributedString class]], @"You must return a valid NSAttributedString object for -titleForEmptyDataSet:");
  126. return string;
  127. }
  128. return nil;
  129. }
  130. - (NSAttributedString *)dzn_detailLabelString
  131. {
  132. if (self.emptyDataSetSource && [self.emptyDataSetSource respondsToSelector:@selector(descriptionForEmptyDataSet:)]) {
  133. NSAttributedString *string = [self.emptyDataSetSource descriptionForEmptyDataSet:self];
  134. if (string) NSAssert([string isKindOfClass:[NSAttributedString class]], @"You must return a valid NSAttributedString object for -descriptionForEmptyDataSet:");
  135. return string;
  136. }
  137. return nil;
  138. }
  139. - (UIImage *)dzn_image
  140. {
  141. if (self.emptyDataSetSource && [self.emptyDataSetSource respondsToSelector:@selector(imageForEmptyDataSet:)]) {
  142. UIImage *image = [self.emptyDataSetSource imageForEmptyDataSet:self];
  143. if (image) NSAssert([image isKindOfClass:[UIImage class]], @"You must return a valid UIImage object for -imageForEmptyDataSet:");
  144. return image;
  145. }
  146. return nil;
  147. }
  148. - (CAAnimation *)dzn_imageAnimation
  149. {
  150. if (self.emptyDataSetSource && [self.emptyDataSetSource respondsToSelector:@selector(imageAnimationForEmptyDataSet:)]) {
  151. CAAnimation *imageAnimation = [self.emptyDataSetSource imageAnimationForEmptyDataSet:self];
  152. if (imageAnimation) NSAssert([imageAnimation isKindOfClass:[CAAnimation class]], @"You must return a valid CAAnimation object for -imageAnimationForEmptyDataSet:");
  153. return imageAnimation;
  154. }
  155. return nil;
  156. }
  157. - (UIColor *)dzn_imageTintColor
  158. {
  159. if (self.emptyDataSetSource && [self.emptyDataSetSource respondsToSelector:@selector(imageTintColorForEmptyDataSet:)]) {
  160. UIColor *color = [self.emptyDataSetSource imageTintColorForEmptyDataSet:self];
  161. if (color) NSAssert([color isKindOfClass:[UIColor class]], @"You must return a valid UIColor object for -imageTintColorForEmptyDataSet:");
  162. return color;
  163. }
  164. return nil;
  165. }
  166. - (NSAttributedString *)dzn_buttonTitleForState:(UIControlState)state
  167. {
  168. if (self.emptyDataSetSource && [self.emptyDataSetSource respondsToSelector:@selector(buttonTitleForEmptyDataSet:forState:)]) {
  169. NSAttributedString *string = [self.emptyDataSetSource buttonTitleForEmptyDataSet:self forState:state];
  170. if (string) NSAssert([string isKindOfClass:[NSAttributedString class]], @"You must return a valid NSAttributedString object for -buttonTitleForEmptyDataSet:forState:");
  171. return string;
  172. }
  173. return nil;
  174. }
  175. - (UIImage *)dzn_buttonImageForState:(UIControlState)state
  176. {
  177. if (self.emptyDataSetSource && [self.emptyDataSetSource respondsToSelector:@selector(buttonImageForEmptyDataSet:forState:)]) {
  178. UIImage *image = [self.emptyDataSetSource buttonImageForEmptyDataSet:self forState:state];
  179. if (image) NSAssert([image isKindOfClass:[UIImage class]], @"You must return a valid UIImage object for -buttonImageForEmptyDataSet:forState:");
  180. return image;
  181. }
  182. return nil;
  183. }
  184. - (UIImage *)dzn_buttonBackgroundImageForState:(UIControlState)state
  185. {
  186. if (self.emptyDataSetSource && [self.emptyDataSetSource respondsToSelector:@selector(buttonBackgroundImageForEmptyDataSet:forState:)]) {
  187. UIImage *image = [self.emptyDataSetSource buttonBackgroundImageForEmptyDataSet:self forState:state];
  188. if (image) NSAssert([image isKindOfClass:[UIImage class]], @"You must return a valid UIImage object for -buttonBackgroundImageForEmptyDataSet:forState:");
  189. return image;
  190. }
  191. return nil;
  192. }
  193. - (UIColor *)dzn_dataSetBackgroundColor
  194. {
  195. if (self.emptyDataSetSource && [self.emptyDataSetSource respondsToSelector:@selector(backgroundColorForEmptyDataSet:)]) {
  196. UIColor *color = [self.emptyDataSetSource backgroundColorForEmptyDataSet:self];
  197. if (color) NSAssert([color isKindOfClass:[UIColor class]], @"You must return a valid UIColor object for -backgroundColorForEmptyDataSet:");
  198. return color;
  199. }
  200. return [UIColor clearColor];
  201. }
  202. - (UIView *)dzn_customView
  203. {
  204. if (self.emptyDataSetSource && [self.emptyDataSetSource respondsToSelector:@selector(customViewForEmptyDataSet:)]) {
  205. UIView *view = [self.emptyDataSetSource customViewForEmptyDataSet:self];
  206. if (view) NSAssert([view isKindOfClass:[UIView class]], @"You must return a valid UIView object for -customViewForEmptyDataSet:");
  207. return view;
  208. }
  209. return nil;
  210. }
  211. - (CGFloat)dzn_verticalOffset
  212. {
  213. CGFloat offset = 0.0;
  214. if (self.emptyDataSetSource && [self.emptyDataSetSource respondsToSelector:@selector(verticalOffsetForEmptyDataSet:)]) {
  215. offset = [self.emptyDataSetSource verticalOffsetForEmptyDataSet:self];
  216. }
  217. return offset;
  218. }
  219. - (CGFloat)dzn_verticalSpace
  220. {
  221. if (self.emptyDataSetSource && [self.emptyDataSetSource respondsToSelector:@selector(spaceHeightForEmptyDataSet:)]) {
  222. return [self.emptyDataSetSource spaceHeightForEmptyDataSet:self];
  223. }
  224. return 0.0;
  225. }
  226. #pragma mark - Delegate Getters & Events (Private)
  227. - (BOOL)dzn_shouldFadeIn {
  228. if (self.emptyDataSetDelegate && [self.emptyDataSetDelegate respondsToSelector:@selector(emptyDataSetShouldFadeIn:)]) {
  229. return [self.emptyDataSetDelegate emptyDataSetShouldFadeIn:self];
  230. }
  231. return YES;
  232. }
  233. - (BOOL)dzn_shouldDisplay
  234. {
  235. if (self.emptyDataSetDelegate && [self.emptyDataSetDelegate respondsToSelector:@selector(emptyDataSetShouldDisplay:)]) {
  236. return [self.emptyDataSetDelegate emptyDataSetShouldDisplay:self];
  237. }
  238. return YES;
  239. }
  240. - (BOOL)dzn_shouldBeForcedToDisplay
  241. {
  242. if (self.emptyDataSetDelegate && [self.emptyDataSetDelegate respondsToSelector:@selector(emptyDataSetShouldBeForcedToDisplay:)]) {
  243. return [self.emptyDataSetDelegate emptyDataSetShouldBeForcedToDisplay:self];
  244. }
  245. return NO;
  246. }
  247. - (BOOL)dzn_isTouchAllowed
  248. {
  249. if (self.emptyDataSetDelegate && [self.emptyDataSetDelegate respondsToSelector:@selector(emptyDataSetShouldAllowTouch:)]) {
  250. return [self.emptyDataSetDelegate emptyDataSetShouldAllowTouch:self];
  251. }
  252. return YES;
  253. }
  254. - (BOOL)dzn_isScrollAllowed
  255. {
  256. if (self.emptyDataSetDelegate && [self.emptyDataSetDelegate respondsToSelector:@selector(emptyDataSetShouldAllowScroll:)]) {
  257. return [self.emptyDataSetDelegate emptyDataSetShouldAllowScroll:self];
  258. }
  259. return NO;
  260. }
  261. - (BOOL)dzn_isImageViewAnimateAllowed
  262. {
  263. if (self.emptyDataSetDelegate && [self.emptyDataSetDelegate respondsToSelector:@selector(emptyDataSetShouldAnimateImageView:)]) {
  264. return [self.emptyDataSetDelegate emptyDataSetShouldAnimateImageView:self];
  265. }
  266. return NO;
  267. }
  268. - (void)dzn_willAppear
  269. {
  270. if (self.emptyDataSetDelegate && [self.emptyDataSetDelegate respondsToSelector:@selector(emptyDataSetWillAppear:)]) {
  271. [self.emptyDataSetDelegate emptyDataSetWillAppear:self];
  272. }
  273. }
  274. - (void)dzn_didAppear
  275. {
  276. if (self.emptyDataSetDelegate && [self.emptyDataSetDelegate respondsToSelector:@selector(emptyDataSetDidAppear:)]) {
  277. [self.emptyDataSetDelegate emptyDataSetDidAppear:self];
  278. }
  279. dispatch_async(dispatch_get_main_queue(), ^{
  280. self.emptyDataSetView.frame = self.bounds;
  281. });
  282. }
  283. - (void)dzn_willDisappear
  284. {
  285. if (self.emptyDataSetDelegate && [self.emptyDataSetDelegate respondsToSelector:@selector(emptyDataSetWillDisappear:)]) {
  286. [self.emptyDataSetDelegate emptyDataSetWillDisappear:self];
  287. }
  288. }
  289. - (void)dzn_didDisappear
  290. {
  291. if (self.emptyDataSetDelegate && [self.emptyDataSetDelegate respondsToSelector:@selector(emptyDataSetDidDisappear:)]) {
  292. [self.emptyDataSetDelegate emptyDataSetDidDisappear:self];
  293. }
  294. }
  295. - (void)dzn_didTapContentView:(id)sender
  296. {
  297. if (self.emptyDataSetDelegate && [self.emptyDataSetDelegate respondsToSelector:@selector(emptyDataSet:didTapView:)]) {
  298. [self.emptyDataSetDelegate emptyDataSet:self didTapView:sender];
  299. }
  300. #pragma clang diagnostic push
  301. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  302. else if (self.emptyDataSetDelegate && [self.emptyDataSetDelegate respondsToSelector:@selector(emptyDataSetDidTapView:)]) {
  303. [self.emptyDataSetDelegate emptyDataSetDidTapView:self];
  304. }
  305. #pragma clang diagnostic pop
  306. }
  307. - (void)dzn_didTapDataButton:(id)sender
  308. {
  309. if (self.emptyDataSetDelegate && [self.emptyDataSetDelegate respondsToSelector:@selector(emptyDataSet:didTapButton:)]) {
  310. [self.emptyDataSetDelegate emptyDataSet:self didTapButton:sender];
  311. }
  312. #pragma clang diagnostic push
  313. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  314. else if (self.emptyDataSetDelegate && [self.emptyDataSetDelegate respondsToSelector:@selector(emptyDataSetDidTapButton:)]) {
  315. [self.emptyDataSetDelegate emptyDataSetDidTapButton:self];
  316. }
  317. #pragma clang diagnostic pop
  318. }
  319. #pragma mark - Setters (Public)
  320. - (void)setEmptyDataSetSource:(id<DZNEmptyDataSetSource>)datasource
  321. {
  322. if (!datasource || ![self dzn_canDisplay]) {
  323. [self dzn_invalidate];
  324. }
  325. objc_setAssociatedObject(self, kEmptyDataSetSource, [[DZNWeakObjectContainer alloc] initWithWeakObject:datasource], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  326. // We add method sizzling for injecting -dzn_reloadData implementation to the native -reloadData implementation
  327. [self swizzleIfPossible:@selector(reloadData)];
  328. // Exclusively for UITableView, we also inject -dzn_reloadData to -endUpdates
  329. if ([self isKindOfClass:[UITableView class]]) {
  330. [self swizzleIfPossible:@selector(endUpdates)];
  331. }
  332. }
  333. - (void)setEmptyDataSetDelegate:(id<DZNEmptyDataSetDelegate>)delegate
  334. {
  335. if (!delegate) {
  336. [self dzn_invalidate];
  337. }
  338. objc_setAssociatedObject(self, kEmptyDataSetDelegate, [[DZNWeakObjectContainer alloc] initWithWeakObject:delegate], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  339. }
  340. #pragma mark - Setters (Private)
  341. - (void)setEmptyDataSetView:(DZNEmptyDataSetView *)view
  342. {
  343. objc_setAssociatedObject(self, kEmptyDataSetView, view, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  344. }
  345. #pragma mark - Reload APIs (Public)
  346. - (void)reloadEmptyDataSet
  347. {
  348. [self dzn_reloadEmptyDataSet];
  349. }
  350. #pragma mark - Reload APIs (Private)
  351. - (void)dzn_reloadEmptyDataSet
  352. {
  353. if (![self dzn_canDisplay]) {
  354. return;
  355. }
  356. if (([self dzn_shouldDisplay] && [self dzn_itemsCount] == 0) || [self dzn_shouldBeForcedToDisplay])
  357. {
  358. // Notifies that the empty dataset view will appear
  359. [self dzn_willAppear];
  360. DZNEmptyDataSetView *view = self.emptyDataSetView;
  361. if (!view.superview) {
  362. // Send the view all the way to the back, in case a header and/or footer is present, as well as for sectionHeaders or any other content
  363. if (([self isKindOfClass:[UITableView class]] || [self isKindOfClass:[UICollectionView class]]) && self.subviews.count > 1) {
  364. [self insertSubview:view atIndex:0];
  365. }
  366. else {
  367. [self addSubview:view];
  368. }
  369. }
  370. // Removing view resetting the view and its constraints it very important to guarantee a good state
  371. [view prepareForReuse];
  372. UIView *customView = [self dzn_customView];
  373. // If a non-nil custom view is available, let's configure it instead
  374. if (customView) {
  375. view.customView = customView;
  376. }
  377. else {
  378. // Get the data from the data source
  379. NSAttributedString *titleLabelString = [self dzn_titleLabelString];
  380. NSAttributedString *detailLabelString = [self dzn_detailLabelString];
  381. UIImage *buttonImage = [self dzn_buttonImageForState:UIControlStateNormal];
  382. NSAttributedString *buttonTitle = [self dzn_buttonTitleForState:UIControlStateNormal];
  383. UIImage *image = [self dzn_image];
  384. UIColor *imageTintColor = [self dzn_imageTintColor];
  385. UIImageRenderingMode renderingMode = imageTintColor ? UIImageRenderingModeAlwaysTemplate : UIImageRenderingModeAlwaysOriginal;
  386. view.verticalSpace = [self dzn_verticalSpace];
  387. // Configure Image
  388. if (image) {
  389. if ([image respondsToSelector:@selector(imageWithRenderingMode:)]) {
  390. view.imageView.image = [image imageWithRenderingMode:renderingMode];
  391. view.imageView.tintColor = imageTintColor;
  392. }
  393. else {
  394. // iOS 6 fallback: insert code to convert imaged if needed
  395. view.imageView.image = image;
  396. }
  397. }
  398. // Configure title label
  399. if (titleLabelString) {
  400. view.titleLabel.attributedText = titleLabelString;
  401. }
  402. // Configure detail label
  403. if (detailLabelString) {
  404. view.detailLabel.attributedText = detailLabelString;
  405. }
  406. // Configure button
  407. if (buttonImage) {
  408. [view.button setImage:buttonImage forState:UIControlStateNormal];
  409. [view.button setImage:[self dzn_buttonImageForState:UIControlStateHighlighted] forState:UIControlStateHighlighted];
  410. }
  411. else if (buttonTitle) {
  412. [view.button setAttributedTitle:buttonTitle forState:UIControlStateNormal];
  413. [view.button setAttributedTitle:[self dzn_buttonTitleForState:UIControlStateHighlighted] forState:UIControlStateHighlighted];
  414. [view.button setBackgroundImage:[self dzn_buttonBackgroundImageForState:UIControlStateNormal] forState:UIControlStateNormal];
  415. [view.button setBackgroundImage:[self dzn_buttonBackgroundImageForState:UIControlStateHighlighted] forState:UIControlStateHighlighted];
  416. }
  417. }
  418. // Configure offset
  419. view.verticalOffset = [self dzn_verticalOffset];
  420. // Configure the empty dataset view
  421. view.backgroundColor = [self dzn_dataSetBackgroundColor];
  422. view.hidden = NO;
  423. view.clipsToBounds = YES;
  424. // Configure empty dataset userInteraction permission
  425. view.userInteractionEnabled = [self dzn_isTouchAllowed];
  426. // Configure empty dataset fade in display
  427. view.fadeInOnDisplay = [self dzn_shouldFadeIn];
  428. [view setupConstraints];
  429. [UIView performWithoutAnimation:^{
  430. [view layoutIfNeeded];
  431. }];
  432. // Configure scroll permission
  433. self.scrollEnabled = [self dzn_isScrollAllowed];
  434. // Configure image view animation
  435. if ([self dzn_isImageViewAnimateAllowed])
  436. {
  437. CAAnimation *animation = [self dzn_imageAnimation];
  438. if (animation) {
  439. [self.emptyDataSetView.imageView.layer addAnimation:animation forKey:kEmptyImageViewAnimationKey];
  440. }
  441. }
  442. else if ([self.emptyDataSetView.imageView.layer animationForKey:kEmptyImageViewAnimationKey]) {
  443. [self.emptyDataSetView.imageView.layer removeAnimationForKey:kEmptyImageViewAnimationKey];
  444. }
  445. // Notifies that the empty dataset view did appear
  446. [self dzn_didAppear];
  447. }
  448. else if (self.isEmptyDataSetVisible) {
  449. [self dzn_invalidate];
  450. }
  451. }
  452. - (void)dzn_invalidate
  453. {
  454. // Notifies that the empty dataset view will disappear
  455. [self dzn_willDisappear];
  456. if (self.emptyDataSetView) {
  457. [self.emptyDataSetView prepareForReuse];
  458. [self.emptyDataSetView removeFromSuperview];
  459. [self setEmptyDataSetView:nil];
  460. }
  461. self.scrollEnabled = YES;
  462. // Notifies that the empty dataset view did disappear
  463. [self dzn_didDisappear];
  464. }
  465. #pragma mark - Method Swizzling
  466. static NSMutableDictionary *_impLookupTable;
  467. static NSString *const DZNSwizzleInfoPointerKey = @"pointer";
  468. static NSString *const DZNSwizzleInfoOwnerKey = @"owner";
  469. static NSString *const DZNSwizzleInfoSelectorKey = @"selector";
  470. // Based on Bryce Buchanan's swizzling technique http://blog.newrelic.com/2014/04/16/right-way-to-swizzle/
  471. // And Juzzin's ideas https://github.com/juzzin/JUSEmptyViewController
  472. void dzn_original_implementation(id self, SEL _cmd)
  473. {
  474. // Fetch original implementation from lookup table
  475. Class baseClass = dzn_baseClassToSwizzleForTarget(self);
  476. NSString *key = dzn_implementationKey(baseClass, _cmd);
  477. NSDictionary *swizzleInfo = [_impLookupTable objectForKey:key];
  478. NSValue *impValue = [swizzleInfo valueForKey:DZNSwizzleInfoPointerKey];
  479. IMP impPointer = [impValue pointerValue];
  480. // We then inject the additional implementation for reloading the empty dataset
  481. // Doing it before calling the original implementation does update the 'isEmptyDataSetVisible' flag on time.
  482. [self dzn_reloadEmptyDataSet];
  483. // If found, call original implementation
  484. if (impPointer) {
  485. ((void(*)(id,SEL))impPointer)(self,_cmd);
  486. }
  487. }
  488. NSString *dzn_implementationKey(Class class, SEL selector)
  489. {
  490. if (!class || !selector) {
  491. return nil;
  492. }
  493. NSString *className = NSStringFromClass([class class]);
  494. NSString *selectorName = NSStringFromSelector(selector);
  495. return [NSString stringWithFormat:@"%@_%@",className,selectorName];
  496. }
  497. Class dzn_baseClassToSwizzleForTarget(id target)
  498. {
  499. if ([target isKindOfClass:[UITableView class]]) {
  500. return [UITableView class];
  501. }
  502. else if ([target isKindOfClass:[UICollectionView class]]) {
  503. return [UICollectionView class];
  504. }
  505. else if ([target isKindOfClass:[UIScrollView class]]) {
  506. return [UIScrollView class];
  507. }
  508. return nil;
  509. }
  510. - (void)swizzleIfPossible:(SEL)selector
  511. {
  512. // Check if the target responds to selector
  513. if (![self respondsToSelector:selector]) {
  514. return;
  515. }
  516. // Create the lookup table
  517. if (!_impLookupTable) {
  518. _impLookupTable = [[NSMutableDictionary alloc] initWithCapacity:3]; // 3 represent the supported base classes
  519. }
  520. // We make sure that setImplementation is called once per class kind, UITableView or UICollectionView.
  521. for (NSDictionary *info in [_impLookupTable allValues]) {
  522. Class class = [info objectForKey:DZNSwizzleInfoOwnerKey];
  523. NSString *selectorName = [info objectForKey:DZNSwizzleInfoSelectorKey];
  524. if ([selectorName isEqualToString:NSStringFromSelector(selector)]) {
  525. if ([self isKindOfClass:class]) {
  526. return;
  527. }
  528. }
  529. }
  530. Class baseClass = dzn_baseClassToSwizzleForTarget(self);
  531. NSString *key = dzn_implementationKey(baseClass, selector);
  532. NSValue *impValue = [[_impLookupTable objectForKey:key] valueForKey:DZNSwizzleInfoPointerKey];
  533. // If the implementation for this class already exist, skip!!
  534. if (impValue || !key || !baseClass) {
  535. return;
  536. }
  537. // Swizzle by injecting additional implementation
  538. Method method = class_getInstanceMethod(baseClass, selector);
  539. IMP dzn_newImplementation = method_setImplementation(method, (IMP)dzn_original_implementation);
  540. // Store the new implementation in the lookup table
  541. NSDictionary *swizzledInfo = @{DZNSwizzleInfoOwnerKey: baseClass,
  542. DZNSwizzleInfoSelectorKey: NSStringFromSelector(selector),
  543. DZNSwizzleInfoPointerKey: [NSValue valueWithPointer:dzn_newImplementation]};
  544. [_impLookupTable setObject:swizzledInfo forKey:key];
  545. }
  546. #pragma mark - UIGestureRecognizerDelegate Methods
  547. - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
  548. {
  549. if ([gestureRecognizer.view isEqual:self.emptyDataSetView]) {
  550. return [self dzn_isTouchAllowed];
  551. }
  552. return [super gestureRecognizerShouldBegin:gestureRecognizer];
  553. }
  554. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
  555. {
  556. UIGestureRecognizer *tapGesture = self.emptyDataSetView.tapGesture;
  557. if ([gestureRecognizer isEqual:tapGesture] || [otherGestureRecognizer isEqual:tapGesture]) {
  558. return YES;
  559. }
  560. // defer to emptyDataSetDelegate's implementation if available
  561. if ( (self.emptyDataSetDelegate != (id)self) && [self.emptyDataSetDelegate respondsToSelector:@selector(gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:)]) {
  562. return [(id)self.emptyDataSetDelegate gestureRecognizer:gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:otherGestureRecognizer];
  563. }
  564. return NO;
  565. }
  566. @end
  567. #pragma mark - DZNEmptyDataSetView
  568. @interface DZNEmptyDataSetView ()
  569. @end
  570. @implementation DZNEmptyDataSetView
  571. @synthesize contentView = _contentView;
  572. @synthesize titleLabel = _titleLabel, detailLabel = _detailLabel, imageView = _imageView, button = _button;
  573. #pragma mark - Initialization Methods
  574. - (instancetype)init
  575. {
  576. self = [super init];
  577. if (self) {
  578. [self addSubview:self.contentView];
  579. }
  580. return self;
  581. }
  582. - (void)didMoveToSuperview
  583. {
  584. self.frame = self.superview.bounds;
  585. void(^fadeInBlock)(void) = ^{_contentView.alpha = 1.0;};
  586. if (self.fadeInOnDisplay) {
  587. [UIView animateWithDuration:0.25
  588. animations:fadeInBlock
  589. completion:NULL];
  590. }
  591. else {
  592. fadeInBlock();
  593. }
  594. }
  595. #pragma mark - Getters
  596. - (UIView *)contentView
  597. {
  598. if (!_contentView)
  599. {
  600. _contentView = [UIView new];
  601. _contentView.translatesAutoresizingMaskIntoConstraints = NO;
  602. _contentView.backgroundColor = [UIColor clearColor];
  603. _contentView.userInteractionEnabled = YES;
  604. _contentView.alpha = 0;
  605. }
  606. return _contentView;
  607. }
  608. - (UIImageView *)imageView
  609. {
  610. if (!_imageView)
  611. {
  612. _imageView = [UIImageView new];
  613. _imageView.translatesAutoresizingMaskIntoConstraints = NO;
  614. _imageView.backgroundColor = [UIColor clearColor];
  615. _imageView.contentMode = UIViewContentModeScaleAspectFit;
  616. _imageView.userInteractionEnabled = NO;
  617. _imageView.accessibilityIdentifier = @"empty set background image";
  618. [_contentView addSubview:_imageView];
  619. }
  620. return _imageView;
  621. }
  622. - (UILabel *)titleLabel
  623. {
  624. if (!_titleLabel)
  625. {
  626. _titleLabel = [UILabel new];
  627. _titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
  628. _titleLabel.backgroundColor = [UIColor clearColor];
  629. _titleLabel.font = [UIFont systemFontOfSize:27.0];
  630. _titleLabel.textColor = [UIColor colorWithWhite:0.6 alpha:1.0];
  631. _titleLabel.textAlignment = NSTextAlignmentCenter;
  632. _titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
  633. _titleLabel.numberOfLines = 0;
  634. _titleLabel.accessibilityIdentifier = @"empty set title";
  635. [_contentView addSubview:_titleLabel];
  636. }
  637. return _titleLabel;
  638. }
  639. - (UILabel *)detailLabel
  640. {
  641. if (!_detailLabel)
  642. {
  643. _detailLabel = [UILabel new];
  644. _detailLabel.translatesAutoresizingMaskIntoConstraints = NO;
  645. _detailLabel.backgroundColor = [UIColor clearColor];
  646. _detailLabel.font = [UIFont systemFontOfSize:17.0];
  647. _detailLabel.textColor = [UIColor colorWithWhite:0.6 alpha:1.0];
  648. _detailLabel.textAlignment = NSTextAlignmentCenter;
  649. _detailLabel.lineBreakMode = NSLineBreakByWordWrapping;
  650. _detailLabel.numberOfLines = 0;
  651. _detailLabel.accessibilityIdentifier = @"empty set detail label";
  652. [_contentView addSubview:_detailLabel];
  653. }
  654. return _detailLabel;
  655. }
  656. - (UIButton *)button
  657. {
  658. if (!_button)
  659. {
  660. _button = [UIButton buttonWithType:UIButtonTypeCustom];
  661. _button.translatesAutoresizingMaskIntoConstraints = NO;
  662. _button.backgroundColor = [UIColor clearColor];
  663. _button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
  664. _button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
  665. _button.accessibilityIdentifier = @"empty set button";
  666. [_button addTarget:self action:@selector(didTapButton:) forControlEvents:UIControlEventTouchUpInside];
  667. [_contentView addSubview:_button];
  668. }
  669. return _button;
  670. }
  671. - (BOOL)canShowImage
  672. {
  673. return (_imageView.image && _imageView.superview);
  674. }
  675. - (BOOL)canShowTitle
  676. {
  677. return (_titleLabel.attributedText.string.length > 0 && _titleLabel.superview);
  678. }
  679. - (BOOL)canShowDetail
  680. {
  681. return (_detailLabel.attributedText.string.length > 0 && _detailLabel.superview);
  682. }
  683. - (BOOL)canShowButton
  684. {
  685. if ([_button attributedTitleForState:UIControlStateNormal].string.length > 0 || [_button imageForState:UIControlStateNormal]) {
  686. return (_button.superview != nil);
  687. }
  688. return NO;
  689. }
  690. #pragma mark - Setters
  691. - (void)setCustomView:(UIView *)view
  692. {
  693. if (!view) {
  694. return;
  695. }
  696. if (_customView) {
  697. [_customView removeFromSuperview];
  698. _customView = nil;
  699. }
  700. _customView = view;
  701. _customView.translatesAutoresizingMaskIntoConstraints = NO;
  702. [self.contentView addSubview:_customView];
  703. }
  704. #pragma mark - Action Methods
  705. - (void)didTapButton:(id)sender
  706. {
  707. SEL selector = NSSelectorFromString(@"dzn_didTapDataButton:");
  708. if ([self.superview respondsToSelector:selector]) {
  709. [self.superview performSelector:selector withObject:sender afterDelay:0.0f];
  710. }
  711. }
  712. - (void)removeAllConstraints
  713. {
  714. [self removeConstraints:self.constraints];
  715. [_contentView removeConstraints:_contentView.constraints];
  716. }
  717. - (void)prepareForReuse
  718. {
  719. [self.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
  720. _titleLabel = nil;
  721. _detailLabel = nil;
  722. _imageView = nil;
  723. _button = nil;
  724. _customView = nil;
  725. [self removeAllConstraints];
  726. }
  727. #pragma mark - Auto-Layout Configuration
  728. - (void)setupConstraints
  729. {
  730. // First, configure the content view constaints
  731. // The content view must alway be centered to its superview
  732. NSLayoutConstraint *centerXConstraint = [self equallyRelatedConstraintWithView:self.contentView attribute:NSLayoutAttributeCenterX];
  733. NSLayoutConstraint *centerYConstraint = [self equallyRelatedConstraintWithView:self.contentView attribute:NSLayoutAttributeCenterY];
  734. [self addConstraint:centerXConstraint];
  735. [self addConstraint:centerYConstraint];
  736. [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[contentView]|" options:0 metrics:nil views:@{@"contentView": self.contentView}]];
  737. // When a custom offset is available, we adjust the vertical constraints' constants
  738. if (self.verticalOffset != 0 && self.constraints.count > 0) {
  739. centerYConstraint.constant = self.verticalOffset;
  740. }
  741. // If applicable, set the custom view's constraints
  742. if (_customView) {
  743. [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[customView]|" options:0 metrics:nil views:@{@"customView":_customView}]];
  744. [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[customView]|" options:0 metrics:nil views:@{@"customView":_customView}]];
  745. }
  746. else {
  747. CGFloat width = CGRectGetWidth(self.frame) ? : CGRectGetWidth([UIScreen mainScreen].bounds);
  748. CGFloat padding = roundf(width/16.0);
  749. CGFloat verticalSpace = self.verticalSpace ? : 11.0; // Default is 11 pts
  750. NSMutableArray *subviewStrings = [NSMutableArray array];
  751. NSMutableDictionary *views = [NSMutableDictionary dictionary];
  752. NSDictionary *metrics = @{@"padding": @(padding)};
  753. // Assign the image view's horizontal constraints
  754. if (_imageView.superview) {
  755. [subviewStrings addObject:@"imageView"];
  756. views[[subviewStrings lastObject]] = _imageView;
  757. [self.contentView addConstraint:[self.contentView equallyRelatedConstraintWithView:_imageView attribute:NSLayoutAttributeCenterX]];
  758. }
  759. // Assign the title label's horizontal constraints
  760. if ([self canShowTitle]) {
  761. [subviewStrings addObject:@"titleLabel"];
  762. views[[subviewStrings lastObject]] = _titleLabel;
  763. [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(padding@750)-[titleLabel(>=0)]-(padding@750)-|"
  764. options:0 metrics:metrics views:views]];
  765. }
  766. // or removes from its superview
  767. else {
  768. [_titleLabel removeFromSuperview];
  769. _titleLabel = nil;
  770. }
  771. // Assign the detail label's horizontal constraints
  772. if ([self canShowDetail]) {
  773. [subviewStrings addObject:@"detailLabel"];
  774. views[[subviewStrings lastObject]] = _detailLabel;
  775. [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(padding@750)-[detailLabel(>=0)]-(padding@750)-|"
  776. options:0 metrics:metrics views:views]];
  777. }
  778. // or removes from its superview
  779. else {
  780. [_detailLabel removeFromSuperview];
  781. _detailLabel = nil;
  782. }
  783. // Assign the button's horizontal constraints
  784. if ([self canShowButton]) {
  785. [subviewStrings addObject:@"button"];
  786. views[[subviewStrings lastObject]] = _button;
  787. [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(padding@750)-[button(>=0)]-(padding@750)-|"
  788. options:0 metrics:metrics views:views]];
  789. }
  790. // or removes from its superview
  791. else {
  792. [_button removeFromSuperview];
  793. _button = nil;
  794. }
  795. NSMutableString *verticalFormat = [NSMutableString new];
  796. // Build a dynamic string format for the vertical constraints, adding a margin between each element. Default is 11 pts.
  797. for (int i = 0; i < subviewStrings.count; i++) {
  798. NSString *string = subviewStrings[i];
  799. [verticalFormat appendFormat:@"[%@]", string];
  800. if (i < subviewStrings.count-1) {
  801. [verticalFormat appendFormat:@"-(%.f@750)-", verticalSpace];
  802. }
  803. }
  804. // Assign the vertical constraints to the content view
  805. if (verticalFormat.length > 0) {
  806. [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"V:|%@|", verticalFormat]
  807. options:0 metrics:metrics views:views]];
  808. }
  809. }
  810. }
  811. - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
  812. {
  813. UIView *hitView = [super hitTest:point withEvent:event];
  814. // Return any UIControl instance such as buttons, segmented controls, switches, etc.
  815. if ([hitView isKindOfClass:[UIControl class]]) {
  816. return hitView;
  817. }
  818. // Return either the contentView or customView
  819. if ([hitView isEqual:_contentView] || [hitView isEqual:_customView]) {
  820. return hitView;
  821. }
  822. return nil;
  823. }
  824. @end
  825. #pragma mark - UIView+DZNConstraintBasedLayoutExtensions
  826. @implementation UIView (DZNConstraintBasedLayoutExtensions)
  827. - (NSLayoutConstraint *)equallyRelatedConstraintWithView:(UIView *)view attribute:(NSLayoutAttribute)attribute
  828. {
  829. return [NSLayoutConstraint constraintWithItem:view
  830. attribute:attribute
  831. relatedBy:NSLayoutRelationEqual
  832. toItem:self
  833. attribute:attribute
  834. multiplier:1.0
  835. constant:0.0];
  836. }
  837. @end
  838. #pragma mark - DZNWeakObjectContainer
  839. @implementation DZNWeakObjectContainer
  840. - (instancetype)initWithWeakObject:(id)object
  841. {
  842. self = [super init];
  843. if (self) {
  844. _weakObject = object;
  845. }
  846. return self;
  847. }
  848. @end