swift 自动布局设置 tableview 的 tableHeaderView 的高度

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, width: kScreenWidth, height: height)
        tabelView.tableHeaderView = v

猜你喜欢

转载自www.cnblogs.com/shen5214444887/p/10723034.html