Collapse a view and all of its subviews (set height = 0) including their margins without using UIStackView

What is the best way (less code = better) to completely hide a view and all of its subviews and at the same time shift related views below it to take its space? Something like what a (vertical) UIStackView does when you set hidden = YES on one of its elements.


I would like to know how this is supposed to be done with Auto Layout without using stack views. People were doing it somehow before UIStackView was added, right? The only thing that comes to mind is making outlets to absolutely ALL height and vertical spacing constraints for all the subviews and set them to zero when I want to collapse the parent view. But this seems like too much code, I thought there surely must be a better way to do that.


My guess is that were as many ways as developers with patience to write the code.


I doubt anyone would approach this by dealing with each leaf subview individually. It would be more sensible to divide them into groups, each of which can be embedded in a container view of some kind (possibly just a non-drawing UIView). Then you can set a container's height to 0 (but you have to beware conflicting constraints inside the container), or remove the container from the view hierarchy (and add a couple of simple constraints between containers when putting it back).


TBH, if you go back far enough to a time where there was autolayout but there wasn't yet UIStackView, the chances are this was done by avoiding autolayout, and manually laying things out in code.


But is there any good reason for avoiding UIStackView?

Collapse a view and all of its subviews (set height = 0) including their margins without using UIStackView
 
 
Q