DZNEmptyDataSet 主要用于 UITableView 以及 UICollectionView 頁(yè)面空白時(shí)展示(也包括UIScrollView)哑姚。
項(xiàng)目中應(yīng)用到此第三方,且用到了自定義視圖眷唉,即實(shí)現(xiàn)以下代理方法
- (UIView *)customViewForEmptyDataSet:(UIScrollView *)scrollView {
return self.emptyView;
}
EmptyView:
- (TJMEmptyView *)emptyView {
if (!_emptyView) {
self.emptyView = [[[NSBundle mainBundle] loadNibNamed:@"TJMEmptyView" owner:self options:nil] firstObject];
_emptyView.frame = CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT);
}
return _emptyView;
}
實(shí)現(xiàn)效果卻如下圖
查看圖層冬阳,原本中間的圖案跑到了最下面
層級(jí)關(guān)系
這個(gè)imageView的父視圖 EmptyView肝陪,及EmptyView的父視圖 高度都為0刑顺。
翻到源碼中查看:
if (_customView) {
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[customView]|" options:0 metrics:nil views:@{@"customView":_customView}]];
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[customView]|" options:0 metrics:nil views:@{@"customView":_customView}]];
}
_customView 即 我自定義的 EmptyView ,contentView 即 EmptyView的父視圖狼讨。contentView使用NSLayoutConstraint 布局,高度依賴于 EmptyView 的高度播聪,未增加約束時(shí)布隔,自然為0,代碼增加一條:
- (TJMEmptyView *)emptyView {
if (!_emptyView) {
self.emptyView = [[[NSBundle mainBundle] loadNibNamed:@"TJMEmptyView" owner:self options:nil] firstObject];
NSLayoutConstraint *heightConstraint = [NSLayoutConstraint constraintWithItem:_emptyView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:TJMScreenHeight];
[_emptyView addConstraint:heightConstraint];
}
return _emptyView;
}
搞定