// // RAEmptyDataView.m // UIScrollViewEmpty // // Created by Jack on 2018/9/5. // Copyright © 2018年 United Software Applications. All rights reserved. // #import "RAEmptyDataView.h" @implementation RAEmptyDataView #pragma mark - Initial - (instancetype)init { if (self = [super init]) { self.backgroundColor = [UIColor clearColor]; // self.translatesAutoresizingMaskIntoConstraints = NO; } return self; } - (void)setFrame:(CGRect)frame { [super setFrame:frame]; } - (void)didMoveToSuperview { [super didMoveToSuperview]; self.frame = self.superview.bounds; } - (void)willMoveToSuperview:(UIView *)newSuperview { [super willMoveToSuperview:newSuperview]; } - (void)setHidden:(BOOL)hidden { [super setHidden:hidden]; } - (void)layoutSubviews { [super layoutSubviews]; if (self.emptyView) { CGFloat w = self.bounds.size.width; CGFloat h = self.bounds.size.height; CGFloat emptyW = self.emptyView.bounds.size.width; CGFloat emptyH = self.emptyView.bounds.size.height; CGFloat x = (w - emptyW) * 0.5; CGFloat y = (h - emptyH) * 0.5; self.emptyView.frame = CGRectMake(x, y, emptyW, emptyH); } } - (void)clear { NSArray *subviews = self.subviews; if (subviews && subviews.count > 0) { for (UIView *subview in subviews) { [subview removeFromSuperview]; } } } - (void)setEmptyView:(UIView *)emptyView { _emptyView = emptyView; [self clear]; [self addSubview:_emptyView]; // NSLayoutConstraint *enterX = [NSLayoutConstraint constraintWithItem:_emptyView // attribute:NSLayoutAttributeCenterX // relatedBy:NSLayoutRelationEqual // toItem:self // attribute:NSLayoutAttributeCenterX // multiplier:1 // constant:0]; // // NSLayoutConstraint *enterY = [NSLayoutConstraint constraintWithItem:_emptyView // attribute:NSLayoutAttributeCenterY // relatedBy:NSLayoutRelationEqual // toItem:self // attribute:NSLayoutAttributeCenterY // multiplier:1 // constant:0]; // // NSLayoutConstraint *w = [NSLayoutConstraint constraintWithItem:_emptyView // attribute:NSLayoutAttributeWidth // relatedBy:NSLayoutRelationEqual // toItem:nil // attribute:NSLayoutAttributeNotAnAttribute // multiplier:1 // constant:100]; // // NSLayoutConstraint *h = [NSLayoutConstraint constraintWithItem:_emptyView // attribute:NSLayoutAttributeHeight // relatedBy:NSLayoutRelationEqual // toItem:nil // attribute:NSLayoutAttributeNotAnAttribute // multiplier:1 // constant:70]; // // // [self addConstraints:@[enterX,enterY,w,h]]; } @end