I am using AutoLayout to position subviews in a UITableViewCell subclass. The view hierarchy is structured like this:

  • Top container view
    • Image view
    • Comment container view
      • Avatar image view
      • Like button
      • Username label
      • Comment button
      • Comment label

When there's an image in the image view, the constraints that I have established work as intended. However, when there is no image and the height of the image view is 0, the comment view fails to fill its container (i.e., its top does not equal the top of its superview). I would expect that if the top of view A is pinned to the bottom of view B and view B has height 0, view A's origin.y would be view B's origin.y. But that is not what's happening. Can anyone tell me what is wrong with my AutoLayout formatting?

    [self.contentContainerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_postImageView(<=200.0)][_commentView]|"
                                                                                  options:0
                                                                                  metrics:nil
                                                                                    views:NSDictionaryOfVariableBindings(_postImageView, _commentView)]];

There are other constraints for the subviews in _commentView and I will post those upon request.

Here's a shot of what's going on:

Gap between top of second cell and top of blue comment container

And the desired effect in the second cell (this is what I get when the format string is modified to :@"|[_commentView]|): Obviously, this breaks the case where an image is present.

enter image description here

有帮助吗?

解决方案

So, there's a constraint in my _commentView with the following format:

@"V:|-(8.0)-[_usernameLabel(30)]-[_contentLabel]|"

_usernameLabel and _contentLabel are both subviews of _commentView. This appears to be the offending constraint. If I remove the final pipe, the _commentView resizes as expected. Unfortunately, without the final pipe, _contentLabel does not expand to reach the bottom of its superview. But that's a separate question.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top