Closed zj381652512 closed 4 years ago
(CGFloat)fd_heightForHeaderFooterViewWithIdentifier:(NSString )identifier configuration:(void (^)(id))configuration { UITableViewHeaderFooterView templateHeaderFooterView = [self fd_templateHeaderFooterViewForReuseIdentifier:identifier];
NSLayoutConstraint *widthFenceConstraint = [NSLayoutConstraint constraintWithItem:templateHeaderFooterView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:CGRectGetWidth(self.frame)]; [templateHeaderFooterView addConstraint:widthFenceConstraint]; CGFloat fittingHeight = [templateHeaderFooterView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height; [templateHeaderFooterView removeConstraint:widthFenceConstraint];
if (fittingHeight == 0) { fittingHeight = [templateHeaderFooterView sizeThatFits:CGSizeMake(CGRectGetWidth(self.frame), 0)].height; } return fittingHeight; }
大佬,用您的库v1.6做仿好友圈,发现计算高度完全不正确,一看没有执行block, header没有被撑起来
如果是xib的cell,会因为从xib生成cell时有默认宽度,会导致计算高度错误,故:
(CGFloat)fd_heightForHeaderFooterViewWithIdentifier:(NSString )identifier configuration:(void (^)(id))configuration { UITableViewHeaderFooterView templateHeaderFooterView = [self fd_templateHeaderFooterViewForReuseIdentifier:identifier];
if_ (configuration) {
configuration(templateHeaderFooterView);
}
NSLayoutConstraint *widthFenceConstraint = [NSLayoutConstraint constraintWithItem:templateHeaderFooterView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:CGRectGetWidth(self.frame)]; [templateHeaderFooterView addConstraint:widthFenceConstraint]; CGFloat fittingHeight = [templateHeaderFooterView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height; [templateHeaderFooterView removeConstraint:widthFenceConstraint];
if (fittingHeight == 0) { fittingHeight = [templateHeaderFooterView sizeThatFits:CGSizeMake(CGRectGetWidth(self.frame), 0)].height; } return fittingHeight; }