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;
}

相关推荐

  1. UITableViewHeader适应方法

    2024-06-07 05:00:04       9 阅读
  2. 解决跨域方法

    2024-06-07 05:00:04       17 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-07 05:00:04       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-07 05:00:04       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-07 05:00:04       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-07 05:00:04       20 阅读

热门阅读

  1. 网络编程介绍(IP)(一)

    2024-06-07 05:00:04       7 阅读
  2. 大型ERP设计-业务与功能指引:物料状态

    2024-06-07 05:00:04       10 阅读
  3. C#中的值类型与引用类型

    2024-06-07 05:00:04       9 阅读
  4. 黑龙江等保测评:强化网络安全的北方防线

    2024-06-07 05:00:04       11 阅读
  5. C# SolidWorks 二次开发-显示配置

    2024-06-07 05:00:04       11 阅读
  6. 【CMake系列】00-CMake学习目录

    2024-06-07 05:00:04       10 阅读
  7. Lf工作流自定义html节点

    2024-06-07 05:00:04       9 阅读
  8. 023、键管理_数据库

    2024-06-07 05:00:04       11 阅读
  9. dubbo服务调用过程

    2024-06-07 05:00:04       12 阅读
  10. 数据计算的基本模式与范式

    2024-06-07 05:00:04       12 阅读