1.固定高度的 tv头部,不根据数据源隐藏某些控件,适用下面的方法

    override init(frame: CGRect) {
super.init(frame: frame)
self.translatesAutoresizingMaskIntoConstraints = false
} override func didMoveToSuperview() {
super.didMoveToSuperview()
if let superV = self.superview{
self.leadingAnchor.constraint(equalTo: superV.leadingAnchor).isActive = true
self.topAnchor.constraint(equalTo: superV.topAnchor).isActive = true
self.trailingAnchor.constraint(equalTo: superV.trailingAnchor).isActive = true
}
}

  使用的时候很简单

    /// tv的 头部
private lazy var tvHeaderView = JYSilverCoinTopView() tv.tableView.tableHeaderView = tvHeaderView
tvHeaderView.delegate = self
tv.translatesAutoresizingMaskIntoConstraints = false

  

2.下面的方法:适用 更具数据源 动态改变 头部高度

核心 :

    /// 重置 tableview的header的frame
private func sizeHeaderToFitForTableHeaderView() {
//FIXME: 这里一定要用过滤,不然会得到默认自带的headerView,高度还不可控
if let headerView = tableView.tableHeaderView {
headerView.setNeedsLayout()
headerView.layoutIfNeeded()
let height = headerView.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize).height
var frame = headerView.frame
frame.size.height = height
frame.size.width = self.frame.size.width
headerView.frame = frame
DDLOG(message: frame)
tableView.tableHeaderView = headerView
}
}
    /// 获取 子控件高度
func sizeHeaderToFit(view:UIView) {
view.setNeedsLayout()
view.layoutIfNeeded()
let width = view.systemLayoutSizeFitting(UILayoutFittingCompressedSize).width
let height = view.systemLayoutSizeFitting(UILayoutFittingCompressedSize).height
}

  

可以参考 :
https://blog.csdn.net/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)
// }
}
}

  

Swift4 - 动态计算UITableView中tableHeaderView的高度 - 获取子控件高度和宽度的更多相关文章

  1. ScrollView子控件高度设置无效

    ScrollView子控件高度设置无效 简述 项目中引入了第三方的下拉刷新包PullToRefreshScrollView. 由于我之前布局未考虑下拉刷新功能.后来暂时发现添加上去,发现.子控件的高度 ...

  2. UITableView的子控件高度不确定处理

    比如,tableView的tableFootView的控件数量是根据网络请求的数据而定的.那么tableView并不能准确的设置其contentSize.处理方法: 在tableFootView的类中 ...

  3. Android 在OnCreate()中获取控件高度与宽度

    试过在OnCreate()中获取控件高度与宽度的童鞋都知道,getWidth()与getHeight()方法返回是0,具体原因 看一下Activity的生命周期 就会明白. 上代码: 方法一: int ...

  4. 获取android控件的高度

    问题 如何获取一个控件的长和高,相信很多朋友第一眼看见这个问题都会觉得很简单,直接在onCreate里面调用getWidth.getMeasuredWidth不就可以获得了吗,但是,事实上是并没有简单 ...

  5. IOS中tableView每组的头部控件、通过tableView的代理方法控制某一行的cell能否达到高亮选中状态

    一.tableView每组的头部控件 1.控件宽度默认就是tableView的宽度 2.控件高度由下面的代理方法决定 - (CGFloat)tableView:(UITableView *)table ...

  6. 动态子类化CComboBox以得到子控件EDIT及LISTBOX

    动态子类化CComboBox以得到子控件EDIT及LISTBOX Joise.LI写于2004-4-6 ComboBox是比较常用的一个控件,有三种样式:CBS_SIMPLE(简单),CBS_DROP ...

  7. [转载]在网页中插入media,RealPlayer等控件

    [转载]在网页中插入media,RealPlayer等控件 (2012-11-02 20:27:43) 转载▼ 标签: 转载   原文地址:在网页中插入media,RealPlayer等控件作者:Mo ...

  8. 【案例分享】在 React 框架中使用 SpreadJS 纯前端表格控件

    [案例分享]在 React 框架中使用 SpreadJS 纯前端表格控件 本期葡萄城公开课,将由国电联合动力技术有限公司,资深前端开发工程师——李林慧女士,与大家在线分享“在 React 框架中使用 ...

  9. jQuery学习笔记(在js中增加、删除及定位控件的操作)

    代码内容很多都是从amazeui直接copy过来的,先声明,请不要说在下抄袭- - <!-------------------- HTML代码 ----------------------> ...

随机推荐

  1. CA单向认证和双向认证的区别?

    1:单向认证,内容会被串改吗?

  2. 6.12-PrepareStatement,JdbcUtil 读取数据库配置文件properties,dao模式

    一.PrepareStatement 防止sql注入 PrepareStatement 是预编译sql语句 更加灵活,更有效率 executeUpdate() 做增删改 executeQuery() ...

  3. 哈希与位图(Hash and BitMap)

    Hash:哈希机制 BitMap:位图机制 目的:都是为了保证检索方便而设置的数据结构 对于大数据进行排序,由于内存限制,不可能在内存中进行,所以采取BitMap机制 为了在大数据中快速检索以及操作数 ...

  4. storm项目优化

    实现监控脚本监控topology运行状态

  5. [Dart] Flutter开发中的几个常用函数

    几个Flutter开发中的常用函数 /** 返回当前时间戳 */ static int currentTimeMillis() { return new DateTime.now().millisec ...

  6. C中运算符

    01,条件表达式, int a = (b>118)?118:a = b; printf("%d\n",a);//指如果b的值是118,则就设置a的值为118,不然就将b的值赋 ...

  7. mysql 解除安全模式

    问题:rror Code: 1175. You are using safe update mode and you tried to update a table without a WHERE t ...

  8. Activity服务类-2 EngineService服务类

    一共提供了9个接口 //获取RepositoryServiceRepositoryService getRepositoryService();//获取RuntimeServiceRuntimeSer ...

  9. python list()总结

      # 1 列表的创建,用方括号表示[ ] name=['xiaolei','xiaoman','lixia','xiaolei'] # 2 列表的查询,通过索引值差值,第一位索引为0 #倒数第一个 ...

  10. UI5-文档-2.4-Node.js-Based开发环境

    用于修改OpenUI5.环境是基于Node.js,用作服务器,具有一个基于Grunt的构建过程.本节提供关于初始设置.开发工作流和测试执行的信息. 常规开发过程: 不需要构建过程,您可以简单地修改任何 ...