2

I am trying to sort out a problem we have in a UITableViewCell. There are nested UIStackViews with subviews like UILabel having an intrinsic content size. I created this example for simplified explanation of my problem:

screenshot of the example storyboard

Results in these interface builder errors:

Top Stack 
View Need constraints for: Y position, height
Middle Stack 
View Need constraints for: Y position, height
Bottom Stack 
View Need constraints for: Y position, height
  • To my (and my collegues) understanding, the stacks' and table view cell sizes should be determined by the UILabels and their intrinsic content size.
  • The "Outer Stack View" has four space constraints to its superview (the "Table View Cell") with 0 as constants because ultimately the table view cell and all stack view sizes should be based on the content views.
  • All UIStackView have Fill for Alignment and Distribution attributes.
  • I just dragged it together from the library as depicted, no fiddling in the inspector. It is simple to reproduce.
  • Adding 0-space top and bottom constraints to a label in relation to its parent stack view does not resolve the stack view allegedly lacking a height constraint.
  • The suggested constraints of the interface builder are nonsense.

What is missing? I created an example project hosted on GitHub.

p13n
  • 859
  • 8
  • 31

1 Answers1

4

You have to set the OutterStackView bottom constraint to be greater than or equal to the bottom of the cells content view.

enter image description here

enter image description here

See the issue is that you are asking the OutterStackView to be the same size as your cell in the StoryBoard but since your internal labels don't have their final intrinsic content size yet, you cannot fulfill all constraint requirements.

javier rivarola
  • 500
  • 3
  • 6
  • According to [the official documentation](https://developer.apple.com/documentation/uikit/uilabel), UILabel does have an intrinsic content size by default. – p13n Jan 28 '20 at 09:17
  • Sorry if i miss use the word, not native speaker, what i meant was that hour outterstack view wants to be a size that it cannot fulfill in the storyboard, only once you call cellForRow, the cell size is calculated correctly, not while in the storyboard – javier rivarola Jan 28 '20 at 10:01
  • A constraint to connect the outer stack view bottom with the cell content view bottom did not solve the issues. Adding a `>= 22` constraint to a label resoves the missing constraint problem for its stack view above. – p13n Feb 03 '20 at 10:10
  • Yes it does, see my screenshot, i used the same layout. – javier rivarola Feb 03 '20 at 10:12
  • I was also facing similar issue. For me adding >= constraints to bottom of outer UIStackView to superview solved the problem. – Raymond Feb 07 '21 at 17:52