Search code examples
iosswiftautolayout

Constraints on a UITableViewHeaderFooterView result in UIViewAlertForUnsatisfiableConstraints


Been starring at this blankly for a bit now, probably too long, so I am trying a post here.

Here's the error I am seeing:

2017-11-02 22:43:23.972361-0700 TableViewCellHeaderViewLayoutTest[88247:17250641] [LayoutConstraints] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
(
    "<NSLayoutConstraint:0x60c000092930 V:|-(12)-[UILabel:0x7fd450c0eb80'Header 1']   (active, names: '|':_UITableViewHeaderFooterContentView:0x7fd450c0f290 )>",
    "<NSLayoutConstraint:0x60c0000927f0 UILabel:0x7fd450c0eb80'Header 1'.bottom == _UITableViewHeaderFooterContentView:0x7fd450c0f290.bottom - 6   (active)>",
    "<NSLayoutConstraint:0x60c000092ac0 'UIView-Encapsulated-Layout-Height' _UITableViewHeaderFooterContentView:0x7fd450c0f290.height == 17.5   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x60c0000927f0 UILabel:0x7fd450c0eb80'Header 1'.bottom == _UITableViewHeaderFooterContentView:0x7fd450c0f290.bottom - 6   (active)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

It's a super simple implementation of my UITableViewHeaderFooterView subclass:

class TableViewSectionHeaderView: UITableViewHeaderFooterView {

    let titleLabel = UILabel()

    override init(reuseIdentifier: String?) {
        super.init(reuseIdentifier: reuseIdentifier)

        contentView.backgroundColor = .gray

        titleLabel.backgroundColor = contentView.backgroundColor
        titleLabel.font = UIFont.preferredFont(forTextStyle: .footnote)

        contentView.addSubview(titleLabel)

        titleLabel.translatesAutoresizingMaskIntoConstraints = false
        titleLabel.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 12).isActive = true
        titleLabel.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 6).isActive = true
        titleLabel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -6).isActive = true
        titleLabel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -6).isActive = true
    }

}

The thing that stands out to me is this constraint:

"<NSLayoutConstraint:0x60c000092ac0 'UIView-Encapsulated-Layout-Height' _UITableViewHeaderFooterContentView:0x7fd450c0f290.height == 17.5   (active)>"

If I inspect it it also has a priority of 1000, which explains why my own constraints seems to fail.

Everything lays out fine visually, but I am concerned about that warning. What am I doing wrong?


Solution

  • It looks like the section header height is competing with the top and bottom constraints of the label, along with the intrinsic height of the actual label. The section header may need to grow or shrink depending on the height of the label, but the strict header height constraint is causing a conflict.

    Set the sectionHeaderHeight and the estimatedSectionHeaderHeight to UITableViewAutomaticDimension to allow the height to adjust to the section's contents (the label in this case).