Установите высоту подпредставления изображения в вертикальном представлении стека - PullRequest
0 голосов
/ 18 июня 2019

У меня есть UITableViewCell, который содержит содержимое в шаблоне с накоплением.

  1. Заголовок (UILabel)
  2. Подзаголовок (UILabel)
  3. Изображение (UIImageView)
  4. Содержимое тела (UILabel)

Изображение не имеет фиксированной высоты, я загружаю его с удаленного ресурса, однако знаю высотуперед тем, как отобразить мой cell, как он включен в исходный ответ API.

Я пытаюсь установить высоту этого изображения на основе значения, которое я держу в модели, однако я продолжаю сталкиваться с autolayout проблемы:

(
    "<NSLayoutConstraint:0x283c90d20 V:|-(8)-[UIStackView:0x1050072a0]   (active, names: '|':UITableViewCellContentView:0x105007ad0 )>",
    "<NSLayoutConstraint:0x283c90e10 UIStackView:0x1050072a0.bottom == UITableViewCellContentView:0x105007ad0.bottom - 8   (active)>",
    "<NSLayoutConstraint:0x283c90eb0 UIImageView:0x1050074a0.height == 220.073   (active)>",
    "<NSLayoutConstraint:0x283cb9720 'UISV-canvas-connection' UIStackView:0x1050072a0.top == UILabel:0x1050034a0'Hey you, get off my cloud'.top   (active)>",
    "<NSLayoutConstraint:0x283cb8910 'UISV-canvas-connection' V:[UIImageView:0x1050074a0]-(0)-|   (active, names: '|':UIStackView:0x1050072a0 )>",
    "<NSLayoutConstraint:0x283cb8190 'UISV-spacing' V:[UILabel:0x1050034a0'Hey you, get off my cloud']-(0)-[UILabel:0x105003790'You don't know me and you...']   (active)>",
    "<NSLayoutConstraint:0x283cb81e0 'UISV-spacing' V:[UILabel:0x105003790'You don't know me and you...']-(0)-[UIImageView:0x1050074a0]   (active)>",
    "<NSLayoutConstraint:0x283cba850 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x105007ad0.height == 16   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x283c90eb0 UIImageView:0x1050074a0.height == 220.073   (active)>

Я выкладываю свой вид программно, что-то вроде этого:

class ArticleBodyCell: UITableViewCell {

    private var articleImageViewHeight: NSLayoutConstraint!

    private let contentStackView = UIStackView(frame: .zero)
    private lazy var articleHeader = UILabel(
        font: theme.font(.header),
        textColor: theme.color(.text),
        numberOfLines: 0
    )
    private lazy var articleSummary = UILabel(
        font: theme.font(.headerSmall),
        textColor: theme.color(.text),
        numberOfLines: 0
    )

    private let articleImageView = UIImageView(frame: .zero)

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        anchorSubViews()
    }

    required init?(coder aDecoder: NSCoder) {
        return nil
    }

    func render(model: ContentArticle?) {
        guard let model = model else { return }

        articleHeader.text = model.title
        articleSummary.text = model.description

        if let asset = model.assets.first, let storageUri = asset?.storageUri {
            articleImageViewHeight.constant = model.heightForArticle

            print("do something w/",storageUri)

        }
    }

    func anchorSubViews() {

        contentView.addSubview(contentStackView)

        contentStackView.translatesAutoresizingMaskIntoConstraints = false

        articleImageViewHeight = articleImageView.heightAnchor.constraint(equalToConstant: 0)
        articleImageViewHeight.isActive = true

        NSLayoutConstraint.activate([
            contentStackView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 8),
            contentStackView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 8),
            contentStackView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -8),
            contentStackView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -8)
        ])

        contentStackView.axis = .vertical
        contentStackView.distribution = .fill

        [articleHeader, articleSummary, articleImageView].forEach { contentStackView.addArrangedSubview($0) }
    }
}

Я думал, что, возможно, захватывая articleImageViewHeight, я мог бы установить высоту, как яобработать модель при визуализации ячейки, но это, похоже, не работает.

РЕДАКТИРОВАТЬ Я также попытался отрегулировать priority моих якорей, используя приведенное ниже, но это не имело никакого эффекта.

contentStackViewBottomAnchor = contentView.bottomAnchor.constraint(equalTo: contentStackView.bottomAnchor)
contentStackViewBottomAnchor.priority = .init(999)
contentStackViewBottomAnchor.isActive = true

1 Ответ

1 голос
/ 18 июня 2019

Установите приоритет на articleImageViewHeight.

Я не верю, что установка на contentView окажет какое-либо влияние.

articleImageViewHeight = articleImageView.heightAnchor.constraint(equalToConstant: 0)
articleImageViewHeight.priority = .init(999)
articleImageViewHeight.isActive = true
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...