Previously do custom headerView to adapt to the height of the system API calculation height, the code is as follows
let size = tableHeaderView.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize)
tableHeaderView.frame = CGRect(origin: CGPoint(x: 0, y: 0), size: size)
listView.tableHeaderView = tableHeaderView
Copy the code
There’s an even easier way to do this by accident. Just set the width constraint of your custom headerView after setting up the tableHeaderView, and you can implement the adaptive height
listView.tableHeaderView = headerView
Copy the code
System constraints
headerView.translatesAutoresizingMaskIntoConstraints = false
headerView.widthAnchor.constraint(equalTo: listView.widthAnchor).isActive = true
Copy the code
Using Snapkit
headerView.snp.makeConstraints { make in
make.width.equalTo(listView)
}
Copy the code
The original link