tableHeaderView 使用autolayout 自适应

UIView *tableHeader = [[UIView alloc] init];
tableHeader.backgroundColor = [UIColor cyanColor];
tableHeader.translatesAutoresizingMaskIntoConstraints = false;

tableView.tableHeaderView = tableHeader;//重要的代码1

UIImageView *bgIV = [[UIImageView alloc] init];
bgIV.contentMode = UIViewContentModeScaleAspectFit;
bgIV.image = [UIImage imageNamed:@"专辑详情背景"];
[tableHeader addSubview:bgIV];
bgIV.translatesAutoresizingMaskIntoConstraints = false;
[tableHeader addConstraint:[NSLayoutConstraint constraintWithItem:tableHeader attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:bgIV attribute:NSLayoutAttributeLeft multiplier:1 constant:0.]];
[tableHeader addConstraint:[NSLayoutConstraint constraintWithItem:tableHeader attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:bgIV attribute:NSLayoutAttributeRight multiplier:1 constant:0.]];
[tableHeader addConstraint:[NSLayoutConstraint constraintWithItem:tableHeader attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:bgIV attribute:NSLayoutAttributeTop multiplier:1 constant:0.]];
[tableHeader addConstraint:[NSLayoutConstraint constraintWithItem:tableHeader attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:bgIV attribute:NSLayoutAttributeBottom multiplier:1 constant:0.]];

UIImageView *logoIV = [[UIImageView alloc] init];
logoIV.contentMode = UIViewContentModeScaleAspectFit;
[logoIV sd_setImageWithURL:[NSURL URLWithString:self.track.coverUrlSmall] placeholderImage:[UIImage imageNamed:@"logoL"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
}];
[tableHeader addSubview:logoIV];

logoIV.translatesAutoresizingMaskIntoConstraints = false;
[tableHeader addConstraint:[NSLayoutConstraint constraintWithItem:tableHeader attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:logoIV attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]];
[tableHeader addConstraint:[NSLayoutConstraint constraintWithItem:tableHeader attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:logoIV attribute:NSLayoutAttributeLeft multiplier:1 constant:15.]];

[tableView.tableHeaderView layoutIfNeeded]; tableView.tableHeaderView = tableHeader; //重要的代码2
参考:TableHeaderView 使用AutoLayout自适应高度

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容