核心 :
/// 获取 子控件高度
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
}
}