I have a stack view
that look like the figure below:
So I change the height of the image
programmatically to make it fit the image tha
Considering that the last constraint says
"<NSLayoutConstraint:0x608000289970 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7f9cbae5a0c0.height == 476.5 (active)>"
I assume you are using stackView
inside of a UITableViewCell
to implement automatic height cells in a tableView
. If my assumption is correct, then the problem is not with the stackView
, nor with the imageView
, but with the way UITableView
works with UITableViewAutomaticDimension
and Autolayout. If the layout works as you expect, and the warning is the only thing that bugs you, then read following.
Therefore it seems to me that this is a result of an known "bug" - there is a collision of the height set by the tableView
and the height calculated by the autolayout. When rendering the cell, the tableView
first applies the default height, calculates the autolayout height, and then use the latter - at least it seems so. See my question. The constraint mentioned above ('UIView-Encapsulated-Layout-Height'
) is the one applied by the UITableView
that later goes away.
That means that the constraints you are using are probably OK. Just set one of the constraints defining height to priority = 999
(so that until it deactivates the default height constraint it won't cause any conflict). In the end, it will result in using your constraint anyway, so it will not cause any layout trouble.
E.g., if you constrain the stackView
to fit the cell's contentView
, set the stackView.bottomAnchor
to contentView.bottomAnchor
just with the priority set to 999. If you did the layout programmatically, this might be your solution:
let bottomConstraint = tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
bottomConstraint.priority = UILayoutPriority(rawValue: 999)
NSLayoutConstraint.activate([
// rest of the constraints
stackView.topAnchor.constraint(equalTo: contentView.topAnchor),
stackView.leftAnchor.constraint(equalTo: contentView.leftAnchor),
stackView.rightAnchor.constraint(equalTo: contentView.rightAnchor),
bottomConstraint,
])
If you do the layout in storyboards, just select appropriate constraint in the storyboards, and in the attributes inspector set its priority to 999 (for example):