Search code examples
iosswiftxcodeautolayoutuistackview

Is there a way to keep subviews of a UIStackView participating in autolayout when they're hidden?


I introduced some UIStackView's in my latest project, because it made the spacing of the views and adding other autolayout constraints a lot easier.

Only to discover that hidden views inside a UIStackView no longer 'participate' when autolayout does its thing.

I suppose this is often a great feature, but for this particular view I don't want that, is there a way to have the subviews of a UIStackView behave as if they were embedded in a plain UIView?
Or do I have no option but to resort to removing the UIStackViews? (and adding a whole lot of annoying 'spacer' views and constraints)


Solution

  • It is by design that hidden arranged subviews are not only hidden but no longer contribute to the layout either. It's a major feature that cannot be achieved easily with auto layout.

    If you want to prevent this, then you can wrap your view within another view. Instead of hiding the direct subview of the UIStackView (the wrapper view in the new setup), hide the inner view (the same way as in the old setup except it is now nested). As the direct subview is visible, UIStackView won't reclaim the space. But the user can't see any content as the view content is hidden.