class headerView: UIView { var whiteView = UIView().then { $0.backgroundColor = UIColor.red } override init(frame: CGRect) { super.init(frame: frame) addSubview(whiteView) whiteView.snp.makeConstraints { (make) in make.left.top.right.equalToSuperview() make.bottom.equalTo(-20) make.height.equalTo(200) } } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func layoutSubviews() { super.layoutSubviews() } }
设置 tableHeaderView 的高度
let v = headerView() let height = CGFloat(v.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize).height) v.frame = CGRect(x: 0, y: 0, kScreenWidth, height: height) tabelView.tableHeaderView = v