Search code examples
iosswiftuitableviewnslayoutconstraint

Issue with constraints inside a UITableViewCell


I have a custom cell for a UITableView. I'd like the following elements inside the cell:

1) UITextView with the following constraints:

  • it starts at the top left corner of the cell
  • it goes from the left side of the cell + 10 until the right side of the cell - 10.
  • its bottom should be 5 points above the next element (see 2 below)

2) UIButton with the following constraints:

  • it is X and Y centered in the cell
  • it goes from the left side of the cell + 20 until the right side of the cell - 20.
  • it has a height of 60

The cell itself is defined with a height of 100.

However it seems my constraints have some conflicts according to the errors I get but I don't see where. Here is my code:


// constraints for the UIButton

answerTextButton.heightAnchor.constraint(equalToConstant: 60).isActive = true
answerTextButton.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 20).isActive = true
answerTextButton.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -20).isActive = true
answerTextButton.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
answerTextButton.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true


// constraints for the UITextView
answerTextView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 10).isActive = true
answerTextView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -10).isActive = true
answerTextView.topAnchor.constraint(equalTo: topAnchor).isActive = true
answerTextView.bottomAnchor.constraint(equalTo: answerTextButton.topAnchor, constant: -5).isActive = true

What's the conflict here?

Thanks.

EDIT : I don't believe my mistake. While you are all right that the X-center constraint is useless, it was not the issue. The issue was... that I forgot to add "answerTextView.translatesAutoresizingMaskIntoConstraints = false". Sorry for that, never happened before! So basically all my constraints for the UITextView were messy because of that. Adding it fixed everything but I kept your recommendation by removing the X-center constraint on the UIButton.


Solution

  • Copy and paste. It will work

           // constraints for the UIButton
    
        answerTextButton.heightAnchor.constraint(equalToConstant: 60).isActive = true
        answerTextButton.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 20).isActive = true
        answerTextButton.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -20).isActive = true
        answerTextButton.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
    
    
          // constraints for the UITextView
        answerTextView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 10).isActive = true
        answerTextView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -10).isActive = true
        answerTextView.topAnchor.constraint(equalTo: topAnchor).isActive = true
        answerTextView.bottomAnchor.constraint(equalTo: answerTextButton.topAnchor, constant: -5).isActive = true