I am trying to add a UIImageView
to a cell and programmatically add auto layout constraints. However, it is giving me this following error: The view hierarchy is not prepared for the constraint... 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.
I looked at these following posts: Swift, Constraint, The view hierarchy is not prepared for the constraint, Why is my layout constraint returning an error in this case? (Swift), and Swift, Constraint, The view hierarchy is not prepared for the constraint. One thing that I could not add to my code that these posts were suggesting me to add is setTranslatesAutoresizingMaskIntoConstraints
. When I try to add this function to my imageView
, I get an error.
Here is my code:
func cellTwo(indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cellTwo", forIndexPath: indexPath) as! CathyTaskLogTwoTableViewCell
let imageView:UIImageView = UIImageView(image: UIImage(named: "defaultPicture"))
imageView.frame.size.width = 100
imageView.frame.size.height = 31
let horizonalContraints = NSLayoutConstraint(item: imageView, attribute:
.LeadingMargin, relatedBy: .Equal, toItem: cell,
attribute: .LeadingMargin, multiplier: 1.0,
constant: 20)
imageView.addConstraint(horizonalContraints)
cell.addSubview(imageView)
return cell
}
Thank you so much in advance for your help :)