酷店

UIScrollView+EmptyDataSet.m 37KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  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. }
  280. - (void)dzn_willDisappear
  281. {
  282. if (self.emptyDataSetDelegate && [self.emptyDataSetDelegate respondsToSelector:@selector(emptyDataSetWillDisappear:)]) {
  283. [self.emptyDataSetDelegate emptyDataSetWillDisappear:self];
  284. }
  285. }
  286. - (void)dzn_didDisappear
  287. {
  288. if (self.emptyDataSetDelegate && [self.emptyDataSetDelegate respondsToSelector:@selector(emptyDataSetDidDisappear:)]) {
  289. [self.emptyDataSetDelegate emptyDataSetDidDisappear:self];
  290. }
  291. }
  292. - (void)dzn_didTapContentView:(id)sender
  293. {
  294. if (self.emptyDataSetDelegate && [self.emptyDataSetDelegate respondsToSelector:@selector(emptyDataSet:didTapView:)]) {
  295. [self.emptyDataSetDelegate emptyDataSet:self didTapView:sender];
  296. }
  297. #pragma clang diagnostic push
  298. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  299. else if (self.emptyDataSetDelegate && [self.emptyDataSetDelegate respondsToSelector:@selector(emptyDataSetDidTapView:)]) {
  300. [self.emptyDataSetDelegate emptyDataSetDidTapView:self];
  301. }
  302. #pragma clang diagnostic pop
  303. }
  304. - (void)dzn_didTapDataButton:(id)sender
  305. {
  306. if (self.emptyDataSetDelegate && [self.emptyDataSetDelegate respondsToSelector:@selector(emptyDataSet:didTapButton:)]) {
  307. [self.emptyDataSetDelegate emptyDataSet:self didTapButton:sender];
  308. }
  309. #pragma clang diagnostic push
  310. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  311. else if (self.emptyDataSetDelegate && [self.emptyDataSetDelegate respondsToSelector:@selector(emptyDataSetDidTapButton:)]) {
  312. [self.emptyDataSetDelegate emptyDataSetDidTapButton:self];
  313. }
  314. #pragma clang diagnostic pop
  315. }
  316. #pragma mark - Setters (Public)
  317. - (void)setEmptyDataSetSource:(id<DZNEmptyDataSetSource>)datasource
  318. {
  319. if (!datasource || ![self dzn_canDisplay]) {
  320. [self dzn_invalidate];
  321. }
  322. objc_setAssociatedObject(self, kEmptyDataSetSource, [[DZNWeakObjectContainer alloc] initWithWeakObject:datasource], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  323. // We add method sizzling for injecting -dzn_reloadData implementation to the native -reloadData implementation
  324. [self swizzleIfPossible:@selector(reloadData)];
  325. // Exclusively for UITableView, we also inject -dzn_reloadData to -endUpdates
  326. if ([self isKindOfClass:[UITableView class]]) {
  327. [self swizzleIfPossible:@selector(endUpdates)];
  328. }
  329. }
  330. - (void)setEmptyDataSetDelegate:(id<DZNEmptyDataSetDelegate>)delegate
  331. {
  332. if (!delegate) {
  333. [self dzn_invalidate];
  334. }
  335. objc_setAssociatedObject(self, kEmptyDataSetDelegate, [[DZNWeakObjectContainer alloc] initWithWeakObject:delegate], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  336. }
  337. #pragma mark - Setters (Private)
  338. - (void)setEmptyDataSetView:(DZNEmptyDataSetView *)view
  339. {
  340. objc_setAssociatedObject(self, kEmptyDataSetView, view, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  341. }
  342. #pragma mark - Reload APIs (Public)
  343. - (void)reloadEmptyDataSet
  344. {
  345. [self dzn_reloadEmptyDataSet];
  346. }
  347. #pragma mark - Reload APIs (Private)
  348. - (void)dzn_reloadEmptyDataSet
  349. {
  350. if (![self dzn_canDisplay]) {
  351. return;
  352. }
  353. if (([self dzn_shouldDisplay] && [self dzn_itemsCount] == 0) || [self dzn_shouldBeForcedToDisplay])
  354. {
  355. // Notifies that the empty dataset view will appear
  356. [self dzn_willAppear];
  357. DZNEmptyDataSetView *view = self.emptyDataSetView;
  358. if (!view.superview) {
  359. // 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
  360. if (([self isKindOfClass:[UITableView class]] || [self isKindOfClass:[UICollectionView class]]) && self.subviews.count > 1) {
  361. [self insertSubview:view atIndex:0];
  362. }
  363. else {
  364. [self addSubview:view];
  365. }
  366. }
  367. // Removing view resetting the view and its constraints it very important to guarantee a good state
  368. [view prepareForReuse];
  369. UIView *customView = [self dzn_customView];
  370. // If a non-nil custom view is available, let's configure it instead
  371. if (customView) {
  372. view.customView = customView;
  373. }
  374. else {
  375. // Get the data from the data source
  376. NSAttributedString *titleLabelString = [self dzn_titleLabelString];
  377. NSAttributedString *detailLabelString = [self dzn_detailLabelString];
  378. UIImage *buttonImage = [self dzn_buttonImageForState:UIControlStateNormal];
  379. NSAttributedString *buttonTitle = [self dzn_buttonTitleForState:UIControlStateNormal];
  380. UIImage *image = [self dzn_image];
  381. UIColor *imageTintColor = [self dzn_imageTintColor];
  382. UIImageRenderingMode renderingMode = imageTintColor ? UIImageRenderingModeAlwaysTemplate : UIImageRenderingModeAlwaysOriginal;
  383. view.verticalSpace = [self dzn_verticalSpace];
  384. // Configure Image
  385. if (image) {
  386. if ([image respondsToSelector:@selector(imageWithRenderingMode:)]) {
  387. view.imageView.image = [image imageWithRenderingMode:renderingMode];
  388. view.imageView.tintColor = imageTintColor;
  389. }
  390. else {
  391. // iOS 6 fallback: insert code to convert imaged if needed
  392. view.imageView.image = image;
  393. }
  394. }
  395. // Configure title label
  396. if (titleLabelString) {
  397. view.titleLabel.attributedText = titleLabelString;
  398. }
  399. // Configure detail label
  400. if (detailLabelString) {
  401. view.detailLabel.attributedText = detailLabelString;
  402. }
  403. // Configure button
  404. if (buttonImage) {
  405. [view.button setImage:buttonImage forState:UIControlStateNormal];
  406. [view.button setImage:[self dzn_buttonImageForState:UIControlStateHighlighted] forState:UIControlStateHighlighted];
  407. }
  408. else if (buttonTitle) {
  409. [view.button setAttributedTitle:buttonTitle forState:UIControlStateNormal];
  410. [view.button setAttributedTitle:[self dzn_buttonTitleForState:UIControlStateHighlighted] forState:UIControlStateHighlighted];
  411. [view.button setBackgroundImage:[self dzn_buttonBackgroundImageForState:UIControlStateNormal] forState:UIControlStateNormal];
  412. [view.button setBackgroundImage:[self dzn_buttonBackgroundImageForState:UIControlStateHighlighted] forState:UIControlStateHighlighted];
  413. }
  414. }
  415. // Configure offset
  416. view.verticalOffset = [self dzn_verticalOffset];
  417. // Configure the empty dataset view
  418. view.backgroundColor = [self dzn_dataSetBackgroundColor];
  419. view.hidden = NO;
  420. view.clipsToBounds = YES;
  421. // Configure empty dataset userInteraction permission
  422. view.userInteractionEnabled = [self dzn_isTouchAllowed];
  423. // Configure empty dataset fade in display
  424. view.fadeInOnDisplay = [self dzn_shouldFadeIn];
  425. [view setupConstraints];
  426. [UIView performWithoutAnimation:^{
  427. [view layoutIfNeeded];
  428. }];
  429. // Configure scroll permission
  430. self.scrollEnabled = [self dzn_isScrollAllowed];
  431. // Configure image view animation
  432. if ([self dzn_isImageViewAnimateAllowed])
  433. {
  434. CAAnimation *animation = [self dzn_imageAnimation];
  435. if (animation) {
  436. [self.emptyDataSetView.imageView.layer addAnimation:animation forKey:kEmptyImageViewAnimationKey];
  437. }
  438. }
  439. else if ([self.emptyDataSetView.imageView.layer animationForKey:kEmptyImageViewAnimationKey]) {
  440. [self.emptyDataSetView.imageView.layer removeAnimationForKey:kEmptyImageViewAnimationKey];
  441. }
  442. // Notifies that the empty dataset view did appear
  443. [self dzn_didAppear];
  444. }
  445. else if (self.isEmptyDataSetVisible) {
  446. [self dzn_invalidate];
  447. }
  448. }
  449. - (void)dzn_invalidate
  450. {
  451. // Notifies that the empty dataset view will disappear
  452. [self dzn_willDisappear];
  453. if (self.emptyDataSetView) {
  454. [self.emptyDataSetView prepareForReuse];
  455. [self.emptyDataSetView removeFromSuperview];
  456. [self setEmptyDataSetView:nil];
  457. }
  458. self.scrollEnabled = YES;
  459. // Notifies that the empty dataset view did disappear
  460. [self dzn_didDisappear];
  461. }
  462. #pragma mark - Method Swizzling
  463. static NSMutableDictionary *_impLookupTable;
  464. static NSString *const DZNSwizzleInfoPointerKey = @"pointer";
  465. static NSString *const DZNSwizzleInfoOwnerKey = @"owner";
  466. static NSString *const DZNSwizzleInfoSelectorKey = @"selector";
  467. // Based on Bryce Buchanan's swizzling technique http://blog.newrelic.com/2014/04/16/right-way-to-swizzle/
  468. // And Juzzin's ideas https://github.com/juzzin/JUSEmptyViewController
  469. void dzn_original_implementation(id self, SEL _cmd)
  470. {
  471. // Fetch original implementation from lookup table
  472. Class baseClass = dzn_baseClassToSwizzleForTarget(self);
  473. NSString *key = dzn_implementationKey(baseClass, _cmd);
  474. NSDictionary *swizzleInfo = [_impLookupTable objectForKey:key];
  475. NSValue *impValue = [swizzleInfo valueForKey:DZNSwizzleInfoPointerKey];
  476. IMP impPointer = [impValue pointerValue];
  477. // We then inject the additional implementation for reloading the empty dataset
  478. // Doing it before calling the original implementation does update the 'isEmptyDataSetVisible' flag on time.
  479. [self dzn_reloadEmptyDataSet];
  480. // If found, call original implementation
  481. if (impPointer) {
  482. ((void(*)(id,SEL))impPointer)(self,_cmd);
  483. }
  484. }
  485. NSString *dzn_implementationKey(Class class, SEL selector)
  486. {
  487. if (!class || !selector) {
  488. return nil;
  489. }
  490. NSString *className = NSStringFromClass([class class]);
  491. NSString *selectorName = NSStringFromSelector(selector);
  492. return [NSString stringWithFormat:@"%@_%@",className,selectorName];
  493. }
  494. Class dzn_baseClassToSwizzleForTarget(id target)
  495. {
  496. if ([target isKindOfClass:[UITableView class]]) {
  497. return [UITableView class];
  498. }
  499. else if ([target isKindOfClass:[UICollectionView class]]) {
  500. return [UICollectionView class];
  501. }
  502. else if ([target isKindOfClass:[UIScrollView class]]) {
  503. return [UIScrollView class];
  504. }
  505. return nil;
  506. }
  507. - (void)swizzleIfPossible:(SEL)selector
  508. {
  509. // Check if the target responds to selector
  510. if (![self respondsToSelector:selector]) {
  511. return;
  512. }
  513. // Create the lookup table
  514. if (!_impLookupTable) {
  515. _impLookupTable = [[NSMutableDictionary alloc] initWithCapacity:3]; // 3 represent the supported base classes
  516. }
  517. // We make sure that setImplementation is called once per class kind, UITableView or UICollectionView.
  518. for (NSDictionary *info in [_impLookupTable allValues]) {
  519. Class class = [info objectForKey:DZNSwizzleInfoOwnerKey];
  520. NSString *selectorName = [info objectForKey:DZNSwizzleInfoSelectorKey];
  521. if ([selectorName isEqualToString:NSStringFromSelector(selector)]) {
  522. if ([self isKindOfClass:class]) {
  523. return;
  524. }
  525. }
  526. }
  527. Class baseClass = dzn_baseClassToSwizzleForTarget(self);
  528. NSString *key = dzn_implementationKey(baseClass, selector);
  529. NSValue *impValue = [[_impLookupTable objectForKey:key] valueForKey:DZNSwizzleInfoPointerKey];
  530. // If the implementation for this class already exist, skip!!
  531. if (impValue || !key || !baseClass) {
  532. return;
  533. }
  534. // Swizzle by injecting additional implementation
  535. Method method = class_getInstanceMethod(baseClass, selector);
  536. IMP dzn_newImplementation = method_setImplementation(method, (IMP)dzn_original_implementation);
  537. // Store the new implementation in the lookup table
  538. NSDictionary *swizzledInfo = @{DZNSwizzleInfoOwnerKey: baseClass,
  539. DZNSwizzleInfoSelectorKey: NSStringFromSelector(selector),
  540. DZNSwizzleInfoPointerKey: [NSValue valueWithPointer:dzn_newImplementation]};
  541. [_impLookupTable setObject:swizzledInfo forKey:key];
  542. }
  543. #pragma mark - UIGestureRecognizerDelegate Methods
  544. - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
  545. {
  546. if ([gestureRecognizer.view isEqual:self.emptyDataSetView]) {
  547. return [self dzn_isTouchAllowed];
  548. }
  549. return [super gestureRecognizerShouldBegin:gestureRecognizer];
  550. }
  551. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
  552. {
  553. UIGestureRecognizer *tapGesture = self.emptyDataSetView.tapGesture;
  554. if ([gestureRecognizer isEqual:tapGesture] || [otherGestureRecognizer isEqual:tapGesture]) {
  555. return YES;
  556. }
  557. // defer to emptyDataSetDelegate's implementation if available
  558. if ( (self.emptyDataSetDelegate != (id)self) && [self.emptyDataSetDelegate respondsToSelector:@selector(gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:)]) {
  559. return [(id)self.emptyDataSetDelegate gestureRecognizer:gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:otherGestureRecognizer];
  560. }
  561. return NO;
  562. }
  563. @end
  564. #pragma mark - DZNEmptyDataSetView
  565. @interface DZNEmptyDataSetView ()
  566. @end
  567. @implementation DZNEmptyDataSetView
  568. @synthesize contentView = _contentView;
  569. @synthesize titleLabel = _titleLabel, detailLabel = _detailLabel, imageView = _imageView, button = _button;
  570. #pragma mark - Initialization Methods
  571. - (instancetype)init
  572. {
  573. self = [super init];
  574. if (self) {
  575. [self addSubview:self.contentView];
  576. }
  577. return self;
  578. }
  579. - (void)didMoveToSuperview
  580. {
  581. self.frame = self.superview.bounds;
  582. void(^fadeInBlock)(void) = ^{_contentView.alpha = 1.0;};
  583. if (self.fadeInOnDisplay) {
  584. [UIView animateWithDuration:0.25
  585. animations:fadeInBlock
  586. completion:NULL];
  587. }
  588. else {
  589. fadeInBlock();
  590. }
  591. }
  592. #pragma mark - Getters
  593. - (UIView *)contentView
  594. {
  595. if (!_contentView)
  596. {
  597. _contentView = [UIView new];
  598. _contentView.translatesAutoresizingMaskIntoConstraints = NO;
  599. _contentView.backgroundColor = [UIColor clearColor];
  600. _contentView.userInteractionEnabled = YES;
  601. _contentView.alpha = 0;
  602. }
  603. return _contentView;
  604. }
  605. - (UIImageView *)imageView
  606. {
  607. if (!_imageView)
  608. {
  609. _imageView = [UIImageView new];
  610. _imageView.translatesAutoresizingMaskIntoConstraints = NO;
  611. _imageView.backgroundColor = [UIColor clearColor];
  612. _imageView.contentMode = UIViewContentModeScaleAspectFit;
  613. _imageView.userInteractionEnabled = NO;
  614. _imageView.accessibilityIdentifier = @"empty set background image";
  615. [_contentView addSubview:_imageView];
  616. }
  617. return _imageView;
  618. }
  619. - (UILabel *)titleLabel
  620. {
  621. if (!_titleLabel)
  622. {
  623. _titleLabel = [UILabel new];
  624. _titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
  625. _titleLabel.backgroundColor = [UIColor clearColor];
  626. _titleLabel.font = [UIFont systemFontOfSize:27.0];
  627. _titleLabel.textColor = [UIColor colorWithWhite:0.6 alpha:1.0];
  628. _titleLabel.textAlignment = NSTextAlignmentCenter;
  629. _titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
  630. _titleLabel.numberOfLines = 0;
  631. _titleLabel.accessibilityIdentifier = @"empty set title";
  632. [_contentView addSubview:_titleLabel];
  633. }
  634. return _titleLabel;
  635. }
  636. - (UILabel *)detailLabel
  637. {
  638. if (!_detailLabel)
  639. {
  640. _detailLabel = [UILabel new];
  641. _detailLabel.translatesAutoresizingMaskIntoConstraints = NO;
  642. _detailLabel.backgroundColor = [UIColor clearColor];
  643. _detailLabel.font = [UIFont systemFontOfSize:17.0];
  644. _detailLabel.textColor = [UIColor colorWithWhite:0.6 alpha:1.0];
  645. _detailLabel.textAlignment = NSTextAlignmentCenter;
  646. _detailLabel.lineBreakMode = NSLineBreakByWordWrapping;
  647. _detailLabel.numberOfLines = 0;
  648. _detailLabel.accessibilityIdentifier = @"empty set detail label";
  649. [_contentView addSubview:_detailLabel];
  650. }
  651. return _detailLabel;
  652. }
  653. - (UIButton *)button
  654. {
  655. if (!_button)
  656. {
  657. _button = [UIButton buttonWithType:UIButtonTypeCustom];
  658. _button.translatesAutoresizingMaskIntoConstraints = NO;
  659. _button.backgroundColor = [UIColor clearColor];
  660. _button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
  661. _button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
  662. _button.accessibilityIdentifier = @"empty set button";
  663. [_button addTarget:self action:@selector(didTapButton:) forControlEvents:UIControlEventTouchUpInside];
  664. [_contentView addSubview:_button];
  665. }
  666. return _button;
  667. }
  668. - (BOOL)canShowImage
  669. {
  670. return (_imageView.image && _imageView.superview);
  671. }
  672. - (BOOL)canShowTitle
  673. {
  674. return (_titleLabel.attributedText.string.length > 0 && _titleLabel.superview);
  675. }
  676. - (BOOL)canShowDetail
  677. {
  678. return (_detailLabel.attributedText.string.length > 0 && _detailLabel.superview);
  679. }
  680. - (BOOL)canShowButton
  681. {
  682. if ([_button attributedTitleForState:UIControlStateNormal].string.length > 0 || [_button imageForState:UIControlStateNormal]) {
  683. return (_button.superview != nil);
  684. }
  685. return NO;
  686. }
  687. #pragma mark - Setters
  688. - (void)setCustomView:(UIView *)view
  689. {
  690. if (!view) {
  691. return;
  692. }
  693. if (_customView) {
  694. [_customView removeFromSuperview];
  695. _customView = nil;
  696. }
  697. _customView = view;
  698. _customView.translatesAutoresizingMaskIntoConstraints = NO;
  699. [self.contentView addSubview:_customView];
  700. }
  701. #pragma mark - Action Methods
  702. - (void)didTapButton:(id)sender
  703. {
  704. SEL selector = NSSelectorFromString(@"dzn_didTapDataButton:");
  705. if ([self.superview respondsToSelector:selector]) {
  706. [self.superview performSelector:selector withObject:sender afterDelay:0.0f];
  707. }
  708. }
  709. - (void)removeAllConstraints
  710. {
  711. [self removeConstraints:self.constraints];
  712. [_contentView removeConstraints:_contentView.constraints];
  713. }
  714. - (void)prepareForReuse
  715. {
  716. [self.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
  717. _titleLabel = nil;
  718. _detailLabel = nil;
  719. _imageView = nil;
  720. _button = nil;
  721. _customView = nil;
  722. [self removeAllConstraints];
  723. }
  724. #pragma mark - Auto-Layout Configuration
  725. - (void)setupConstraints
  726. {
  727. // First, configure the content view constaints
  728. // The content view must alway be centered to its superview
  729. NSLayoutConstraint *centerXConstraint = [self equallyRelatedConstraintWithView:self.contentView attribute:NSLayoutAttributeCenterX];
  730. NSLayoutConstraint *centerYConstraint = [self equallyRelatedConstraintWithView:self.contentView attribute:NSLayoutAttributeCenterY];
  731. [self addConstraint:centerXConstraint];
  732. [self addConstraint:centerYConstraint];
  733. [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[contentView]|" options:0 metrics:nil views:@{@"contentView": self.contentView}]];
  734. // When a custom offset is available, we adjust the vertical constraints' constants
  735. if (self.verticalOffset != 0 && self.constraints.count > 0) {
  736. centerYConstraint.constant = self.verticalOffset;
  737. }
  738. // If applicable, set the custom view's constraints
  739. if (_customView) {
  740. [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[customView]|" options:0 metrics:nil views:@{@"customView":_customView}]];
  741. [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[customView]|" options:0 metrics:nil views:@{@"customView":_customView}]];
  742. }
  743. else {
  744. CGFloat width = CGRectGetWidth(self.frame) ? : CGRectGetWidth([UIScreen mainScreen].bounds);
  745. CGFloat padding = roundf(width/16.0);
  746. CGFloat verticalSpace = self.verticalSpace ? : 11.0; // Default is 11 pts
  747. NSMutableArray *subviewStrings = [NSMutableArray array];
  748. NSMutableDictionary *views = [NSMutableDictionary dictionary];
  749. NSDictionary *metrics = @{@"padding": @(padding)};
  750. // Assign the image view's horizontal constraints
  751. if (_imageView.superview) {
  752. [subviewStrings addObject:@"imageView"];
  753. views[[subviewStrings lastObject]] = _imageView;
  754. [self.contentView addConstraint:[self.contentView equallyRelatedConstraintWithView:_imageView attribute:NSLayoutAttributeCenterX]];
  755. }
  756. // Assign the title label's horizontal constraints
  757. if ([self canShowTitle]) {
  758. [subviewStrings addObject:@"titleLabel"];
  759. views[[subviewStrings lastObject]] = _titleLabel;
  760. [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(padding@750)-[titleLabel(>=0)]-(padding@750)-|"
  761. options:0 metrics:metrics views:views]];
  762. }
  763. // or removes from its superview
  764. else {
  765. [_titleLabel removeFromSuperview];
  766. _titleLabel = nil;
  767. }
  768. // Assign the detail label's horizontal constraints
  769. if ([self canShowDetail]) {
  770. [subviewStrings addObject:@"detailLabel"];
  771. views[[subviewStrings lastObject]] = _detailLabel;
  772. [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(padding@750)-[detailLabel(>=0)]-(padding@750)-|"
  773. options:0 metrics:metrics views:views]];
  774. }
  775. // or removes from its superview
  776. else {
  777. [_detailLabel removeFromSuperview];
  778. _detailLabel = nil;
  779. }
  780. // Assign the button's horizontal constraints
  781. if ([self canShowButton]) {
  782. [subviewStrings addObject:@"button"];
  783. views[[subviewStrings lastObject]] = _button;
  784. [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(padding@750)-[button(>=0)]-(padding@750)-|"
  785. options:0 metrics:metrics views:views]];
  786. }
  787. // or removes from its superview
  788. else {
  789. [_button removeFromSuperview];
  790. _button = nil;
  791. }
  792. NSMutableString *verticalFormat = [NSMutableString new];
  793. // Build a dynamic string format for the vertical constraints, adding a margin between each element. Default is 11 pts.
  794. for (int i = 0; i < subviewStrings.count; i++) {
  795. NSString *string = subviewStrings[i];
  796. [verticalFormat appendFormat:@"[%@]", string];
  797. if (i < subviewStrings.count-1) {
  798. [verticalFormat appendFormat:@"-(%.f@750)-", verticalSpace];
  799. }
  800. }
  801. // Assign the vertical constraints to the content view
  802. if (verticalFormat.length > 0) {
  803. [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"V:|%@|", verticalFormat]
  804. options:0 metrics:metrics views:views]];
  805. }
  806. }
  807. }
  808. - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
  809. {
  810. UIView *hitView = [super hitTest:point withEvent:event];
  811. // Return any UIControl instance such as buttons, segmented controls, switches, etc.
  812. if ([hitView isKindOfClass:[UIControl class]]) {
  813. return hitView;
  814. }
  815. // Return either the contentView or customView
  816. if ([hitView isEqual:_contentView] || [hitView isEqual:_customView]) {
  817. return hitView;
  818. }
  819. return nil;
  820. }
  821. @end
  822. #pragma mark - UIView+DZNConstraintBasedLayoutExtensions
  823. @implementation UIView (DZNConstraintBasedLayoutExtensions)
  824. - (NSLayoutConstraint *)equallyRelatedConstraintWithView:(UIView *)view attribute:(NSLayoutAttribute)attribute
  825. {
  826. return [NSLayoutConstraint constraintWithItem:view
  827. attribute:attribute
  828. relatedBy:NSLayoutRelationEqual
  829. toItem:self
  830. attribute:attribute
  831. multiplier:1.0
  832. constant:0.0];
  833. }
  834. @end
  835. #pragma mark - DZNWeakObjectContainer
  836. @implementation DZNWeakObjectContainer
  837. - (instancetype)initWithWeakObject:(id)object
  838. {
  839. self = [super init];
  840. if (self) {
  841. _weakObject = object;
  842. }
  843. return self;
  844. }
  845. @end