UIViewController in UIStackView

Hello

I have a viewController (A) with a stackView in it. The stackView has constraints for leading, trailing and bottom.

Inside the stackView, I add a UIViewContainer, that embeds a UIViewController (B). B contains a UIView, with a fixed height, and top, bottom, leading and trailing constraints.

When I launch the app, the stackView has a height of 0, as if the contained viewController had no constraints.

Is there a way to have the stackView size be correct without adding a constraint for the stackView height ?

Thanks
Answered by Engineer in 613394022

The stackView has constraints for leading, trailing and bottom.

What you describe here could very well be the issue — every view’s constraints must be unambiguous. Otherwise, runtime behaviour could be unpredictable. For example, if you were creating a plain rectangular view, you could:
  1. Give it an explicit width, height, x, and y positions, or

  2. Constrain it to the top, bottom, leading, and trailing edges of the superview.

Either one of these options fully define its size. (And a quick note, the latter is often preferable because it allows the view to scale when its content changes — for example, if the user has enabled accessibility options such as Dynamic Text or is running the app in a UI language other than English.)

Now, imagine your plain rectangle is defined by with constraints relative to its superview (the second option above). What if you were to remove the constraint at the top? What would happen? How tall is the view? It could be super tall beyond the top of the device’s canvas, or it could be super short.

So I think you may just need to add a constraint from the the top of the stack view to its parent.

However, I could be wrong, since I can’t see the app. You can try and confirm your theory in a few ways:
  • At runtime, do you see any error messages in the Xcode console about ambiguous constraints?

  • If you take a look in the view debugger, does the stack view actually have a height of zero, or is something else going on?

Debugging Auto Layout in the Auto Layout Guide could be helpful here, as well as Debugging View Hierarchies.
The issue may be caused by the fact that the VC (B) inside it will not have set its size until after the VC (B) has set its layout after viewDidAppear was called. You may want to check what the height of the VC (B) is inside VC (A)'s viewDidAppear, and if that is still 0, you may need to setup a delegate from the VC (B) viewDidAppear to send the height after auto layout has finished up. If you are able to get the height that way, you could then manually setup the height of the stack view.

An easier solution would be to not use a VC inside a stack view, and instead use a UIView, if that is possible for your design at all.
Accepted Answer

The stackView has constraints for leading, trailing and bottom.

What you describe here could very well be the issue — every view’s constraints must be unambiguous. Otherwise, runtime behaviour could be unpredictable. For example, if you were creating a plain rectangular view, you could:
  1. Give it an explicit width, height, x, and y positions, or

  2. Constrain it to the top, bottom, leading, and trailing edges of the superview.

Either one of these options fully define its size. (And a quick note, the latter is often preferable because it allows the view to scale when its content changes — for example, if the user has enabled accessibility options such as Dynamic Text or is running the app in a UI language other than English.)

Now, imagine your plain rectangle is defined by with constraints relative to its superview (the second option above). What if you were to remove the constraint at the top? What would happen? How tall is the view? It could be super tall beyond the top of the device’s canvas, or it could be super short.

So I think you may just need to add a constraint from the the top of the stack view to its parent.

However, I could be wrong, since I can’t see the app. You can try and confirm your theory in a few ways:
  • At runtime, do you see any error messages in the Xcode console about ambiguous constraints?

  • If you take a look in the view debugger, does the stack view actually have a height of zero, or is something else going on?

Debugging Auto Layout in the Auto Layout Guide could be helpful here, as well as Debugging View Hierarchies.
Hi

I understand what you're saying.

However, I want my stackView to grow based on its own subviews (who have all the constraints set).
I suspect the problem you are having is that typically the parent view controller sets the size and position of the child view controller. You can have the child view controller communicate its preferred size to the parent by having it (the child) set its preferredContentSize. You then implement preferredContentSizeDidChange in the parent view controller to react to the change.

So you might have an optional height constraint in the parent view controller that you update with the preferred content size of the child view.


UIViewController in UIStackView
 
 
Q