I recently struggled with auto layout errors by hiding UIStackView . Instead of doing a bunch of book storage and wrapping in UIViews , I decided to create an output for my parentStackView and outputs for the children I want to hide / show.
@IBOutlet weak var parentStackView: UIStackView! @IBOutlet var stackViewNumber1: UIStackView! @IBOutlet var stackViewNumber2: UIStackView!
In the storyboard, what my parent table looks like:

It has 4 children, and each of the children has a bunch of stack views inside them. When you hide the view of the stack, if it receives user interface elements, which are also representations of the stack, you will see a stream of errors in the automatic layout. Instead of hiding, I decided to remove them.
In my example, parentStackViews contains an array of 4 elements: Top Stack View, StackViewNumber1, Stack View Number 2, and Stop Button. Their indices in arrangedSubviews are 0, 1, 2, and 3, respectively. When I want to hide it, I just delete it from the parentStackView's arrangedSubviews array. Since it is not weak, it lingers in memory, and you can simply return it back to your desired index later. I do not reinitialize it, so it just hangs until it is needed, but it does not inflate memory.
So basically, you can ...
1) Drag and drop IBOutlets for your parent stack and the children you want to hide / show in the storyboard.
2) If you want to hide them, delete the stack that you want to hide from the parentStackView's arrangedSubviews array.
3) Call self.view.layoutIfNeeded() using UIView.animateWithDuration .
Note that the last two elements of stackViews are not weak . You need to keep them around when you show them.
Let's say I want to hide stackViewNumber2:
parentStackView.removeArrangedSubview(stackViewNumber2) stackViewNumber2.removeFromSuperview()
Then we animate it:
UIView.animate(withDuration: 0.25, delay: 0, usingSpringWithDamping: 2.0, initialSpringVelocity: 10.0, options: [.curveEaseOut], animations: { self.view.layoutIfNeeded() }, completion: nil)
If you want to “show” stackViewNumber2 later, you can simply insert it into the desired parentStackView arrangedSubviews index and spice up the update.
parentStackView.removeArrangedSubview(stackViewNumber1) stackViewNumber1.removeFromSuperview() parentStackView.insertArrangedSubview(stackViewNumber2, at: 1)
I found this to be a lot easier than doing constraint bookkeeping, tinkering with priorities, etc.
If you have something that you want to hide by default, you can simply put it on the storyboard and delete it in viewDidLoad and update it without animation using view.layoutIfNeeded() .