RAEmptyDataView.m 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //
  2. // RAEmptyDataView.m
  3. // UIScrollViewEmpty
  4. //
  5. // Created by Jack on 2018/9/5.
  6. // Copyright © 2018年 United Software Applications. All rights reserved.
  7. //
  8. #import "RAEmptyDataView.h"
  9. @implementation RAEmptyDataView
  10. #pragma mark - Initial
  11. - (instancetype)init {
  12. if (self = [super init]) {
  13. self.backgroundColor = [UIColor clearColor];
  14. // self.translatesAutoresizingMaskIntoConstraints = NO;
  15. }
  16. return self;
  17. }
  18. - (void)setFrame:(CGRect)frame {
  19. [super setFrame:frame];
  20. }
  21. - (void)didMoveToSuperview {
  22. [super didMoveToSuperview];
  23. self.frame = self.superview.bounds;
  24. }
  25. - (void)willMoveToSuperview:(UIView *)newSuperview {
  26. [super willMoveToSuperview:newSuperview];
  27. }
  28. - (void)setHidden:(BOOL)hidden {
  29. [super setHidden:hidden];
  30. }
  31. - (void)layoutSubviews {
  32. [super layoutSubviews];
  33. if (self.emptyView) {
  34. CGFloat w = self.bounds.size.width;
  35. CGFloat h = self.bounds.size.height;
  36. CGFloat emptyW = self.emptyView.bounds.size.width;
  37. CGFloat emptyH = self.emptyView.bounds.size.height;
  38. CGFloat x = (w - emptyW) * 0.5;
  39. CGFloat y = (h - emptyH) * 0.5;
  40. self.emptyView.frame = CGRectMake(x, y, emptyW, emptyH);
  41. }
  42. }
  43. - (void)clear {
  44. NSArray *subviews = self.subviews;
  45. if (subviews && subviews.count > 0) {
  46. for (UIView *subview in subviews) {
  47. [subview removeFromSuperview];
  48. }
  49. }
  50. }
  51. - (void)setEmptyView:(UIView *)emptyView {
  52. _emptyView = emptyView;
  53. [self clear];
  54. [self addSubview:_emptyView];
  55. // NSLayoutConstraint *enterX = [NSLayoutConstraint constraintWithItem:_emptyView
  56. // attribute:NSLayoutAttributeCenterX
  57. // relatedBy:NSLayoutRelationEqual
  58. // toItem:self
  59. // attribute:NSLayoutAttributeCenterX
  60. // multiplier:1
  61. // constant:0];
  62. //
  63. // NSLayoutConstraint *enterY = [NSLayoutConstraint constraintWithItem:_emptyView
  64. // attribute:NSLayoutAttributeCenterY
  65. // relatedBy:NSLayoutRelationEqual
  66. // toItem:self
  67. // attribute:NSLayoutAttributeCenterY
  68. // multiplier:1
  69. // constant:0];
  70. //
  71. // NSLayoutConstraint *w = [NSLayoutConstraint constraintWithItem:_emptyView
  72. // attribute:NSLayoutAttributeWidth
  73. // relatedBy:NSLayoutRelationEqual
  74. // toItem:nil
  75. // attribute:NSLayoutAttributeNotAnAttribute
  76. // multiplier:1
  77. // constant:100];
  78. //
  79. // NSLayoutConstraint *h = [NSLayoutConstraint constraintWithItem:_emptyView
  80. // attribute:NSLayoutAttributeHeight
  81. // relatedBy:NSLayoutRelationEqual
  82. // toItem:nil
  83. // attribute:NSLayoutAttributeNotAnAttribute
  84. // multiplier:1
  85. // constant:70];
  86. //
  87. //
  88. // [self addConstraints:@[enterX,enterY,w,h]];
  89. }
  90. @end