• UITableViewHeader自适应的几种方法


    目录

    前言

    1.使用 Auto Layout

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

    3.计算高度并手动设置 

    4.使用自定义 UIView 子类


    前言

            这篇文章主要介绍UITableViewHeaderView自适应的几种方法。

    1.使用 Auto Layout

            最常用且推荐的方法是使用 Auto Layout 设置表头视图的高度。以下是具体实现步骤:

    1. #import "ViewController.h"
    2. @interface ViewController ()
    3. @property (nonatomic, strong) UITableView *tableView;
    4. @end
    5. @implementation ViewController
    6. - (void)viewDidLoad {
    7. [super viewDidLoad];
    8. self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    9. [self.view addSubview:self.tableView];
    10. UIView *headerView = [self createTableHeaderView];
    11. self.tableView.tableHeaderView = headerView;
    12. }
    13. - (UIView *)createTableHeaderView {
    14. UIView *headerView = [[UIView alloc] init];
    15. UILabel *label = [[UILabel alloc] init];
    16. label.text = @"This is a dynamic header view. It can have multiple lines and the height will adjust accordingly.";
    17. label.numberOfLines = 0;
    18. label.translatesAutoresizingMaskIntoConstraints = NO;
    19. [headerView addSubview:label];
    20. [NSLayoutConstraint activateConstraints:@[
    21. [label.leadingAnchor constraintEqualToAnchor:headerView.leadingAnchor constant:15],
    22. [label.trailingAnchor constraintEqualToAnchor:headerView.trailingAnchor constant:-15],
    23. [label.topAnchor constraintEqualToAnchor:headerView.topAnchor constant:15],
    24. [label.bottomAnchor constraintEqualToAnchor:headerView.bottomAnchor constant:-15]
    25. ]];
    26. // 触发布局以计算高度
    27. [headerView setNeedsLayout];
    28. [headerView layoutIfNeeded];
    29. // 获取动态高度
    30. CGFloat height = [headerView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
    31. CGRect headerFrame = headerView.frame;
    32. headerFrame.size.height = height;
    33. headerView.frame = headerFrame;
    34. return headerView;
    35. }
    36. @end

    2.使用sizeThatFits            

            如果不使用 Auto Layout,可以通过实现 sizeThatFits:方法来手动计算表头视图的高度。

    实例代码如下:

    1. - (UIView *)createTableHeaderView {
    2. UIView *headerView = [[UIView alloc] init];
    3. UILabel *label = [[UILabel alloc] init];
    4. label.text = @"This is a dynamic header view. It can have multiple lines and the height will adjust accordingly.";
    5. label.numberOfLines = 0;
    6. label.frame = CGRectMake(15, 15, self.view.bounds.size.width - 30, 0);
    7. [label sizeToFit];
    8. [headerView addSubview:label];
    9. CGRect headerFrame = headerView.frame;
    10. headerFrame.size.height = CGRectGetMaxY(label.frame) + 15;
    11. headerView.frame = headerFrame;
    12. return headerView;
    13. }

    3.计算高度并手动设置 

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

    实例代码如下:

    1. - (UIView *)createTableHeaderView {
    2. UIView *headerView = [[UIView alloc] init];
    3. UILabel *label = [[UILabel alloc] init];
    4. label.text = @"This is a dynamic header view. It can have multiple lines and the height will adjust accordingly.";
    5. label.numberOfLines = 0;
    6. CGSize maxSize = CGSizeMake(self.view.bounds.size.width - 30, CGFLOAT_MAX);
    7. CGSize requiredSize = [label sizeThatFits:maxSize];
    8. label.frame = CGRectMake(15, 15, maxSize.width, requiredSize.height);
    9. [headerView addSubview:label];
    10. CGRect headerFrame = headerView.frame;
    11. headerFrame.size.height = requiredSize.height + 30;
    12. headerView.frame = headerFrame;
    13. return headerView;
    14. }

    4.使用自定义 UIView 子类

            可以创建一个自定义的 UIView 子类,并在其中处理布局和高度计算。

    1. @interface CustomTableHeaderView : UIView
    2. @property (nonatomic, strong) UILabel *label;
    3. @end
    4. @implementation CustomTableHeaderView
    5. - (instancetype)init {
    6. self = [super init];
    7. if (self) {
    8. self.label = [[UILabel alloc] init];
    9. self.label.text = @"This is a dynamic header view. It can have multiple lines and the height will adjust accordingly.";
    10. self.label.numberOfLines = 0;
    11. [self addSubview:self.label];
    12. }
    13. return self;
    14. }
    15. - (void)layoutSubviews {
    16. [super layoutSubviews];
    17. CGSize maxSize = CGSizeMake(self.bounds.size.width - 30, CGFLOAT_MAX);
    18. CGSize requiredSize = [self.label sizeThatFits:maxSize];
    19. self.label.frame = CGRectMake(15, 15, maxSize.width, requiredSize.height);
    20. CGRect frame = self.frame;
    21. frame.size.height = requiredSize.height + 30;
    22. self.frame = frame;
    23. }
    24. @end

            使用自定义UIView子类。

    1. - (void)viewDidLoad {
    2. [super viewDidLoad];
    3. self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    4. [self.view addSubview:self.tableView];
    5. CustomTableHeaderView *headerView = [[CustomTableHeaderView alloc] init];
    6. headerView.frame = CGRectMake(0, 0, self.view.bounds.size.width, 0);
    7. [headerView layoutIfNeeded]; // 触发 layoutSubviews 计算高度
    8. self.tableView.tableHeaderView = headerView;
    9. }
  • 相关阅读:
    Windows按键win和alt不小心互换了
    uniapp实战项目 (仿知识星球App) - - 利用computed监听用户操作
    基于SSM SpringBoot vue个人博客网站
    SELF-INSTRUCT: Aligning Language Models with Self-Generated Instructions
    Serverless 架构落地实践及案例解析
    Linux下IIC驱动编写,介绍IIC子系统框架的使用
    CommonsCollection4反序列化链学习
    Android随笔-ClassLoader
    加上boot程序,FreeRTOS就跑不起来了
    故障诊断实验台 | PT500mini轴承齿轮箱转子故障实验台
  • 原文地址:https://blog.csdn.net/ZCC361571217/article/details/139382253