5

Here is my following code:

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let headerView = UIView()
    headerView.backgroundColor = UIColor.white
    let headerLabel = UILabel(frame: CGRect(x: 30, y: 0, width:
      tableView.bounds.size.width, height: tableView.bounds.size.height))
    headerLabel.font = UIFont().robotoMedium(withFontSize: 10)
    headerLabel.textColor = CustomColor.lightGrey.color
    headerLabel.text = "Travel Shops"
    headerLabel.sizeToFit()
    headerView.addSubview(headerLabel)
    headerLabel.translatesAutoresizingMaskIntoConstraints = false

    headerLabel.addConstraint(NSLayoutConstraint(item: headerLabel, attribute: .trailing, relatedBy: .equal, toItem: tableView, attribute: .trailing, multiplier: 1, constant: 0))
    headerLabel.addConstraint(NSLayoutConstraint(item: headerLabel, attribute: .leading, relatedBy: .equal, toItem: tableView, attribute: .leading, multiplier: 1, constant: 0))

    headerLabel.addConstraint(NSLayoutConstraint(item: headerLabel, attribute: .top, relatedBy: .equal, toItem: self.topLayoutGuide, attribute: .bottom, multiplier: 1, constant: 0))
    headerLabel.addConstraint(NSLayoutConstraint(item: headerLabel, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 131))
    return headerView
  }

But the application crashes with the following:

When added to a view, the constraint's items must be descendants of that view (or the view itself). This will crash if the constraint needs to be resolved before the view hierarchy is assembled. Break on -[UIView(UIConstraintBasedLayout) _viewHierarchyUnpreparedForConstraint:] to debug.

Chelsea Shawra
  • 1,593
  • 4
  • 22
  • 43

1 Answers1

6

headerLabel is child view of headerView and headerView is child of TableView in the hierarchy that is why this message is shown you need add the constraints to headerView which is the headerLabel.superView instead of UITableView which is headerLabel.superView.superView

Change your code

    headerLabel.addConstraint(NSLayoutConstraint(item: headerLabel, attribute: .trailing, relatedBy: .equal, toItem: headerView, attribute: .trailing, multiplier: 1, constant: 0))
    headerLabel.addConstraint(NSLayoutConstraint(item: headerLabel, attribute: .leading, relatedBy: .equal, toItem: headerView, attribute: .leading, multiplier: 1, constant: 0))

    headerLabel.addConstraint(NSLayoutConstraint(item: headerLabel, attribute: .top, relatedBy: .equal, toItem: self.topLayoutGuide, attribute: .bottom, multiplier: 1, constant: 0))
    headerLabel.addConstraint(NSLayoutConstraint(item: headerLabel, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 131))

by this one

    headerLabel.addConstraint(NSLayoutConstraint(item: headerLabel, attribute: .trailing, relatedBy: .equal, toItem: tableView, attribute: .trailing, multiplier: 1, constant: 0))
    headerLabel.addConstraint(NSLayoutConstraint(item: headerLabel, attribute: .leading, relatedBy: .equal, toItem: headerView, attribute: .leading, multiplier: 1, constant: 0))

    headerLabel.addConstraint(NSLayoutConstraint(item: headerLabel, attribute: .top, relatedBy: .equal, toItem: headerView, attribute: .bottom, multiplier: 1, constant: 0))
    headerLabel.addConstraint(NSLayoutConstraint(item: headerLabel, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 131))

Hope this helps

Reinier Melian
  • 20,519
  • 3
  • 38
  • 55