9

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)

Pieter
  • 17,435
  • 8
  • 50
  • 89

2 Answers2

15

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.

Codo
  • 75,595
  • 17
  • 168
  • 206
  • on a simple testproject this seems to do the trick, thanks a lot – Pieter Aug 24 '16 at 07:16
  • 1
    This solution works perfectly and it is really easy to use. Because your view has already all the proper outlets, just by Embedding into a new view and setting the constraints this will have the intentend effect. Thanks! – Felipe Ferri Jun 20 '18 at 21:32
6

Instead of hiding the subview, you can just turn its alpha to 0. This way the subview won't be visible but it will participate in the layout.

manueGE
  • 1,169
  • 11
  • 14
  • Some points to mention: A hidden view does not listen to touch events. Even alpha below zero level means it's invisible, and Apple doesn't like invisible being touchable. Using alpha requires that the graphics hardware blend each pixel and it's compute-intensive. – pedrouan Sep 22 '16 at 13:46