目录
前言
这篇文章主要介绍UITableViewHeaderView自适应的几种方法。
1.使用 Auto Layout
最常用且推荐的方法是使用 Auto Layout 设置表头视图的高度。以下是具体实现步骤:
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic, strong) UITableView *tableView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
[self.view addSubview:self.tableView];
UIView *headerView = [self createTableHeaderView];
self.tableView.tableHeaderView = headerView;
}
- (UIView *)createTableHeaderView {
UIView *headerView = [[UIView alloc] init];
UILabel *label = [[UILabel alloc] init];
label.text = @"This is a dynamic header view. It can have multiple lines and the height will adjust accordingly.";
label.numberOfLines = 0;
label.translatesAutoresizingMaskIntoConstraints = NO;
[headerView addSubview:label];
[NSLayoutConstraint activateConstraints:@[
[label.leadingAnchor constraintEqualToAnchor:headerView.leadingAnchor constant:15],
[label.trailingAnchor constraintEqualToAnchor:headerView.trailingAnchor constant:-15],
[label.topAnchor constraintEqualToAnchor:headerView.topAnchor constant:15],
[label.bottomAnchor constraintEqualToAnchor:headerView.bottomAnchor constant:-15]
]];
// 触发布局以计算高度
[headerView setNeedsLayout];
[headerView layoutIfNeeded];
// 获取动态高度
CGFloat height = [headerView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
CGRect headerFrame = headerView.frame;
headerFrame.size.height = height;
headerView.frame = headerFrame;
return headerView;
}
@end
2.使用sizeThatFits
如果不使用 Auto Layout,可以通过实现 sizeThatFits:
方法来手动计算表头视图的高度。
实例代码如下:
- (UIView *)createTableHeaderView {
UIView *headerView = [[UIView alloc] init];
UILabel *label = [[UILabel alloc] init];
label.text = @"This is a dynamic header view. It can have multiple lines and the height will adjust accordingly.";
label.numberOfLines = 0;
label.frame = CGRectMake(15, 15, self.view.bounds.size.width - 30, 0);
[label sizeToFit];
[headerView addSubview:label];
CGRect headerFrame = headerView.frame;
headerFrame.size.height = CGRectGetMaxY(label.frame) + 15;
headerView.frame = headerFrame;
return headerView;
}
3.计算高度并手动设置
另一种方法是提前计算表头视图的高度,然后手动设置 tableHeaderView
的 frame。
实例代码如下:
- (UIView *)createTableHeaderView {
UIView *headerView = [[UIView alloc] init];
UILabel *label = [[UILabel alloc] init];
label.text = @"This is a dynamic header view. It can have multiple lines and the height will adjust accordingly.";
label.numberOfLines = 0;
CGSize maxSize = CGSizeMake(self.view.bounds.size.width - 30, CGFLOAT_MAX);
CGSize requiredSize = [label sizeThatFits:maxSize];
label.frame = CGRectMake(15, 15, maxSize.width, requiredSize.height);
[headerView addSubview:label];
CGRect headerFrame = headerView.frame;
headerFrame.size.height = requiredSize.height + 30;
headerView.frame = headerFrame;
return headerView;
}
4.使用自定义 UIView 子类
可以创建一个自定义的 UIView 子类,并在其中处理布局和高度计算。
@interface CustomTableHeaderView : UIView
@property (nonatomic, strong) UILabel *label;
@end
@implementation CustomTableHeaderView
- (instancetype)init {
self = [super init];
if (self) {
self.label = [[UILabel alloc] init];
self.label.text = @"This is a dynamic header view. It can have multiple lines and the height will adjust accordingly.";
self.label.numberOfLines = 0;
[self addSubview:self.label];
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
CGSize maxSize = CGSizeMake(self.bounds.size.width - 30, CGFLOAT_MAX);
CGSize requiredSize = [self.label sizeThatFits:maxSize];
self.label.frame = CGRectMake(15, 15, maxSize.width, requiredSize.height);
CGRect frame = self.frame;
frame.size.height = requiredSize.height + 30;
self.frame = frame;
}
@end
使用自定义UIView子类。
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
[self.view addSubview:self.tableView];
CustomTableHeaderView *headerView = [[CustomTableHeaderView alloc] init];
headerView.frame = CGRectMake(0, 0, self.view.bounds.size.width, 0);
[headerView layoutIfNeeded]; // 触发 layoutSubviews 计算高度
self.tableView.tableHeaderView = headerView;
}