Swift4 - 动态计算UITableView中tableHeaderView的高度 - 获取子控件高度和宽度

本文介绍了一种在SwiftUI应用中实现HeaderView高度自适应的方法。通过`systemLayoutSizeFitting`计算视图的理想尺寸,并更新HeaderView的高度,确保其在不同数据量下也能正确显示。文中提供了一个具体的示例,展示了如何在一个包含TableView的自定义UIView中实施这一方案。

 核心 : 

    /// 获取 子控件高度
    func sizeHeaderToFit(view:UIView)  {
        view.setNeedsLayout()
        view.layoutIfNeeded()
        let width = view.systemLayoutSizeFitting(UILayoutFittingCompressedSize).width
        let height = view.systemLayoutSizeFitting(UILayoutFittingCompressedSize).height
    }

  

可以参考 :
https://blog.youkuaiyun.com/longshihua/article/details/78595502

 

1. headerView 设置

import UIKit

class JYNewCardDetailHeaderV: UIView {

    /// 储值卡总耗卡
    let dyczkzhkLabel = UILabel(text: "储值卡总耗卡", fontSize: 16, isSetBoldFontSize: true, textColor: UIColor.init(hexColor: "424242"), textAlignment: .left)
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func configData(){
        
    }
}
extension JYNewCardDetailHeaderV{
    
    func setupUI() {

    //VFL或者 layout设置 要把这个设置为false , snapkit 正常设置,不写的话界面也正常,就是报约束错误
        self.translatesAutoresizingMaskIntoConstraints = false

        let vd : [String:UIView] = ["dyczkzhkLabel":dyczkzhkLabel
        vd.fastAddToView(self)
        self.fastAddConstraints("|[dyczkzhkLabel]|", vd)
        self.fastAddConstraints("V:|-10-[dyczkzhkLabel]-10-|", vd)
    }
}

  

2. 使用这个view的地方配置

import Foundation

private let cellID = "JYNewCardDetailCell"
class JYStatmentCarView: UIView {
    
   1.  //创建headerV
    let headerV = JYNewCardDetailHeaderV(frame: CGRect.zero)

   //创建tableview
    fileprivate lazy var tableView : UITableView = {
        let tableView = UITableView.init(frame: CGRect.zero, style: .plain)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.separatorStyle = .none
        tableView.register(JYNewCardDetailCell.self, forCellReuseIdentifier: cellID)
        return tableView
    }()
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.translatesAutoresizingMaskIntoConstraints = false
        self.configUI()
    }
    

//3.重新计算header的frame
    override func layoutSubviews() {
        super.layoutSubviews()
        sizeHeaderToFit()
    }
    
    
    /// 重置 tableview的header的frame
    func sizeHeaderToFit() {
        let headerView = tableView.tableHeaderView
        
        headerView?.setNeedsLayout()
        // 立马布局子视图
        headerView?.layoutIfNeeded()
        
        let height = headerView?.systemLayoutSizeFitting(UILayoutFittingCompressedSize).height ?? 0
        var frame = headerView?.frame ?? CGRect.zero
        frame.size.height = height
        headerView?.frame = frame
        // 重新设置tableHeaderView
        tableView.tableHeaderView = headerView
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    /// 布局UI
    private func configUI() {
        DDLOG(message: "创建卡相关")
        
        let vd : [String:UIView] = ["tableView":tableView]
        let metrics: [String: Any] = [ "DeviceWidth": JY_DEVICE_WIDTH]
        vd.fastAddToView(self)
        self.fastAddConstraints("|[tableView(DeviceWidth)]|", vd, [], metrics)
        self.fastAddConstraints("V:|[tableView]|", vd, [], metrics)
        

    //2.设置tableview的headerView, 并且一定设置headerV的约束
        tableView.tableHeaderView = headerV
    
    //VFL设置 headerV.topAnchor.constraint(equalTo: headerV.superview?.topAnchor ?? tableView.topAnchor).fastActive() headerV.leftAnchor.constraint(equalTo: self.leftAnchor).fastActive() headerV.rightAnchor.constraint(equalTo: self.rightAnchor).fastActive()

    //snapkit 设置 // headerV.snp.makeConstraints { (make) in // make.top.equalToSuperview() // make.left.right.equalTo(self) // } } } // MARK:- 代理 extension JYStatmentCarView:UITableViewDelegate , UITableViewDataSource { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 10 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView .dequeueReusableCell(withIdentifier: cellID, for: indexPath) as! JYNewCardDetailCell cell.configData() return cell } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return UITableViewAutomaticDimension } func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat { return UITableViewAutomaticDimension } }

  

转载于:https://www.cnblogs.com/qingzZ/p/9669901.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值