Sean Zlatnik Sean Zlatnik -3 years ago 120
iOS Question

Making a UITextView work inside a UIStackView

I'm trying to add some UITextViews to a UIStackView and the text views aren't showing up. I've researched this issue and the #1 answer I found was to make

UITextViews.enableScrolling
set to
false
. However, when I do that, the text view becomes very long. It goes off the view controller and the UIStackView also becomes very wide and goes off beyond the edge of the view controller. Also (and this is true) all the other content in the UIStackView disappears. I think this is pretty strange.

This is a vertical stack view, by the way. I wish I better understood why the UIStackView was behaving this way. Any ideas on how to get the stack view to behave as expected would be most appreciated. By the way, the UITextViews are not editable and don't have to respond to user interaction. Below is the storyboard and the stack settings.

View Controller Stack Settings

At this point there are no constraints being used with the stack view or its contents. I've updated the spacing between items to be 8, but that makes no difference- I've tried all numbers 0-8 and the UITextViews don't appear with any value. Otherwise, I've basically left the UIStackView values alone.

Answer Source

I don't have enough rep to comment, but here's what I would suggest: Within the Stack View, you can explicitly set constraints for each object in the stack view. Try setting all the height constraints & width (using pin to sides, or explicit width) so that each object knows how to be distributed.

From https://developer.apple.com/documentation/uikit/uistackview:

For all distributions except the fillEqually distribution, the stack view uses each arranged view’s intrinsicContentSize property when calculating its size along the stack’s axis.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download