UITableViewHeader自适应的几种方法

目录

前言

1.使用 Auto Layout

2.使用sizeThatFits:方法手动计算头视图的高度                

3.计算高度并手动设置 

4.使用自定义 UIView 子类


前言

        这篇文章主要介绍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.计算高度并手动设置 

        另一种方法是提前计算表头视图的高度,然后手动设置 tableHeaderViewframe。

实例代码如下:

- (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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我叫柱子哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值