Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When added to a view, the constraint's items must be descendants of that view (or the view itself).

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.

like image 680
Chelsea Shawra Avatar asked Jul 19 '17 18:07

Chelsea Shawra


People also ask

How do I define the position of a view in constraintlayout?

To define a view's position in ConstraintLayout, you must add at least one horizontal and one vertical constraint for the view. Each constraint represents a connection or alignment to another view, the parent layout, or an invisible guideline.

How many constraints do I need for a view?

Each constraint defines the view's position along either the vertical or horizontal axis; so each view must have a minimum of one constraint for each axis, but often more are necessary. When you drop a view into the Layout Editor, it stays where you leave it even if it has no constraints.

What are the constraintlayout constraints in AutoCAD?

Constraints overview. To define a view's position in ConstraintLayout, you must add at least one horizontal and one vertical constraint for the view. Each constraint represents a connection or alignment to another view, the parent layout, or an invisible guideline.

Which type of constraint must be created at the table level?

A constraint for a composite primary key must be created at the table level. true Which of the following types of constraints will not allow NULL values? PRIMARY KEY


1 Answers

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

like image 97
Reinier Melian Avatar answered Oct 19 '22 04:10

Reinier Melian