There are numerous threads about correctly sizing a tableHeaderView with auto-layout (one such thread) but they tend to pre-date iOS 8.
I have a situation with numerous
Since this question is a year and a half old, here is a updated and complete version, in Swift. Some of the code from the accepted answer is wrong or outdated.
func fixHeaderHeight() {
// Set your label text if needed
// ...
//
guard let header = tableView.tableHeaderView else {
return
}
let height = header.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize).height
header.frame.height = height
tableView.tableHeaderView = header
}
Somewhere else in your code, you'll need to set the preferredMaxLayoutWidth
of the label(s) in the header. This should be equal to the tableView (or screen width) minus any padding. The didSet
method of your label outlet is a good place:
@IBOutlet weak var headerMessageLabel: UILabel! {
didSet {
headerMessageLabel.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.width - headerMessageLabelPadding
}
}
Note: If the accepted answer worked for you, you aren't using size classes properly.