muyexi

Fix layout issue for tableHeaderView

At times, there's some strange auto layout issue with tableHeaderView, the subview is either compressed or stretched. I check and re-check all constraints were set up correctly, it's especially frustrating. Here a container view come to rescue, the auto layout gets back to order if I put it in a container view.

extension UITableView {
    // Fix headerView resize
    func setupHeaderView(_ headerView: UIView) {
        let headerContainerView = UIView(frame: headerView.frame)
        headerContainerView.addSubview(headerView)
        
        tableHeaderView = headerContainerView
        
        headerView.snp.makeConstraints { (make) -> Void in
            make.edges.equalTo(headerContainerView).inset(UIEdgeInsets.zero)
        }
    }
}

评论