UIView+ZYLEmptyView.m 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. //
  2. // UIView+ZYLEmptyView.m
  3. // mask
  4. //
  5. // Created by 翟玉磊 on 2018/12/9.
  6. // Copyright © 2018 翟玉磊. All rights reserved.
  7. //
  8. #import "UIView+ZYLEmptyView.h"
  9. /// 标识 showPosterGraph
  10. static char ZYL_EMPTY_VIEW;
  11. @implementation UIView (ZYLEmptyView)
  12. #pragma mark - Getter & Setter
  13. - (void)setEmptyDataView:(ZYLEmptyDataView *)emptyDataView{
  14. [self willChangeValueForKey:@"emptyDataView"];
  15. //关联对象
  16. objc_setAssociatedObject(self, &ZYL_EMPTY_VIEW,
  17. emptyDataView,
  18. OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  19. [self didChangeValueForKey:@"emptyDataView"];
  20. }
  21. - (ZYLEmptyDataView *)emptyDataView{
  22. return objc_getAssociatedObject(self, &ZYL_EMPTY_VIEW);
  23. }
  24. #pragma mark - Method
  25. - (void)zyl_configEmptyViewWithType:(ZYLEmptyDataViewType)type emptyInfo:(NSString *)emptyInfo errorInfo:(NSString *)errorInfo offsetTop:(CGFloat)offsetTop emptyDataViewTop:(CGFloat)emptyDataViewTop hasData:(BOOL)hasData hasError:(BOOL)hasError reloadBlock:(void(^)(void))reloadBlock {
  26. [self zyl_configEmptyViewWithType:type emptyInfo:emptyInfo errorInfo:errorInfo offsetTop:offsetTop hasData:hasData hasError:hasError reloadBlock:reloadBlock];
  27. if (self.emptyDataView) {
  28. self.emptyDataView.frame = CGRectMake(self.emptyDataView.f_x, emptyDataViewTop, self.emptyDataView.f_width, self.emptyDataView.f_heigh);
  29. }
  30. }
  31. - (void)zyl_configEmptyViewWithType:(ZYLEmptyDataViewType)type emptyInfo:(nullable NSString *)emptyInfo errorInfo:(nullable NSString *)errorInfo offsetTop:(CGFloat)offsetTop bgColor:(nullable UIColor *)bgColor hasData:(BOOL)hasData hasError:(BOOL)hasError reloadBlock:(nullable void(^)(void))reloadBlock {
  32. [self zyl_configEmptyViewWithType:type emptyInfo:emptyInfo errorInfo:errorInfo offsetTop:offsetTop hasData:hasData hasError:hasError reloadBlock:reloadBlock];
  33. if (bgColor) {
  34. self.emptyDataView.backgroundColor = bgColor;
  35. }
  36. }
  37. - (void)zyl_configEmptyViewWithType:(ZYLEmptyDataViewType)type emptyInfo:(NSString *)emptyInfo errorInfo:(NSString *)errorInfo offsetTop:(CGFloat)offsetTop hasData:(BOOL)hasData hasError:(BOOL)hasError reloadBlock:(void(^)(void))reloadBlock{
  38. if (hasData) { /// 有数据,则不需要显示占位图
  39. self.emptyDataView.hidden = YES;
  40. [self.emptyDataView removeFromSuperview];
  41. return;
  42. }
  43. /// 没有数据的情况 1. 确实没有数据 2. 请求出错导致无数据
  44. if (self.emptyDataView == nil) {
  45. /// 懒加载
  46. /// CoderMikeHe Fixed Bug : 这里设置`self.emptyDataView`的尺寸不要直接使用`superviewBounds`,因为`UIScrollView`的子类其`bounds`有可能不是(0 , 0 , width , height)这种值
  47. CGRect superviewBounds = self.bounds;
  48. CGFloat width = CGRectGetWidth(superviewBounds)==0?SCREEN_WIDTH:CGRectGetWidth(superviewBounds);
  49. CGFloat height = CGRectGetHeight(superviewBounds)==0?SCREEN_HEIGHT:CGRectGetHeight(superviewBounds);
  50. self.emptyDataView = [[ZYLEmptyDataView alloc] initWithFrame:CGRectMake(0.0, 0.0, width, height)];
  51. }
  52. if (!self.emptyDataView.superview) {
  53. // 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
  54. if (([self isKindOfClass:[UITableView class]] || [self isKindOfClass:[UICollectionView class]]) && self.subviews.count > 1) {
  55. [self insertSubview:self.emptyDataView atIndex:0];
  56. }
  57. else {
  58. [self addSubview:self.emptyDataView];
  59. }
  60. }
  61. self.emptyDataView.hidden = NO;
  62. self.emptyDataView.backgroundColor = Color_Clear;
  63. /// 配置占位视图的内容
  64. [self.emptyDataView configEmptyViewWithType:type emptyInfo:emptyInfo errorInfo:errorInfo offsetTop:offsetTop hasData:hasData hasError:hasError reloadBlock:reloadBlock];
  65. }
  66. @end