【问题标题】:Updating the Height Constraint of a View Inside of a UIStackView更新 UIStackView 内部视图的高度约束
【发布时间】:2016-06-14 09:13:59
【问题描述】:

目前,我设置了我稍后添加到堆栈视图中的一个视图的高度和宽度约束,如下所示:(供您参考,productsTable 是一个 UITableView。)

    productsTable.heightAnchor.constraintEqualToConstant(tableHeight).active = true
    productsTable.widthAnchor.constraintEqualToConstant(stackWidth).active = true
    stackView.insertArrangedSubview(productsTable, atIndex: productsTableIndex)

稍后,在 ViewWillAppear 中,我想更改 productsTable 的高度,如下所示:

productsTable.heightAnchor.constraintEqualToConstant(newHeight).active = true.

尽管如此,表视图在(更改/更新)ViewWillAppear 中的约束之后保持相同的大小。有什么我做错了或者可以做不同的事情来达到预期的效果吗?

【问题讨论】:

  • 你应该修改productsTable.heightAnchor而不是productsTable.widthAnchor吗?
  • 很抱歉,我写这个问题的时候打错了,但是是的,这并不能解决问题。

标签: swift dynamic height constraints stackview


【解决方案1】:

您需要保留对第一次创建的高度约束的引用。

let heightConstraint = productsTable.heightAnchor.constraintEqualToConstant(tableHeight)
heightConstraint.active = true

稍后,在 viewWillAppear() 中,您将能够直接将此约束的常量属性设置为 newHeight。

heightConstraint.constant = newHeight

【讨论】:

  • 如果这能解决您的问题,您能否为答案投票?谢谢;)
  • 嘿,特里,我会的,但显然在我获得至少 15 的声誉之前我无法使用它