Проблема с ограничениями внутри UITableViewCell - PullRequest
1 голос
/ 01 ноября 2019

У меня есть пользовательская ячейка для UITableView. Я хотел бы, чтобы следующие элементы внутри ячейки:

1) UITextView со следующими ограничениями:

  • он начинается в верхнем левом углу ячейки
  • она идет от левой стороны ячейки + 10 до правой стороны ячейки - 10.
  • ее нижняя часть должна быть на 5 пунктов выше следующего элемента (см. 2 ниже)

2) Кнопка UIB со следующими ограничениями:

  • это X и Y центрированы в ячейке
  • она идет от левой стороны ячейки + 20 до правой стороныячейка - 20.
  • она имеет высоту 60

Сама ячейка определена с высотой 100.

Однако, похоже, у моих ограничений есть некоторые конфликтыпо ошибкам я получаю но не вижу где. Вот мой код:


// 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

В чем здесь конфликт?

Спасибо.

РЕДАКТИРОВАТЬ: я не верю своей ошибке. Хотя с вами все в порядке, что ограничение X-center бесполезно, но проблема не в этом. Проблема была ... что я забыл добавить "answerTextView.translatesAutoresizingMaskIntoConstraints = false". Извините за это, никогда не случалось раньше! Так что в основном все мои ограничения для UITextView были грязными из-за этого. Добавление этого исправило все, но я сохранил вашу рекомендацию, удалив ограничение X-center на UIButton.

1 Ответ

0 голосов
/ 01 ноября 2019

Копирование и вставка. Это будет работать

       // 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
...