How do I set the height of tableHeaderView (UITableView) with autolayout?

后端 未结 12 1270
后悔当初
后悔当初 2020-11-27 10:23

I\'m been smashing my head against the wall with this for last 3 or 4 hours and I can\'t seem to figure it out. I have a UIViewController with a full screen UITableView insi

12条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-27 10:54

    It works for both header view and footer just replace the header with footer

    func sizeHeaderToFit() {
        if let headerView = tableView.tableHeaderView {
    
            headerView.setNeedsLayout()
            headerView.layoutIfNeeded()
    
            let height = headerView.systemLayoutSizeFitting(UILayoutFittingCompressedSize).height
            var frame = headerView.frame
            frame.size.height = height
            headerView.frame = frame
    
            tableView.tableHeaderView = headerView
        }
    }
    

提交回复
热议问题