You could try to remove the view from the StackView to hide and reinsert in correct position to show.
Get details here: https://stackoverflow.com/questions/30728062/add-views-in-uistackview-programmatically
Which iOS version ?
They say here you can just hide instead of remove, but that's where you have the problem:
https://stackoverflow.com/questions/41483641/remove-views-in-uistackview-swift
In UIStackView definition in Xcode (cmd-click on UIStackView - Jump to definition:
/* Spacing between adjacent edges of arrangedSubviews.
Used as a strict spacing for the Fill distributions, and
as a minimum spacing for the EqualCentering and EqualSpacing
distributions. Use negative values to allow overlap.
On iOS 11.0 or later, use UIStackViewSpacingUseSystem (Swift: UIStackView.spacingUseSystem)
to get a system standard spacing value. Setting spacing to UIStackViewSpacingUseDefault
(Swift: UIStackView.spacingUseDefault) will result in a spacing of 0.
System spacing between views depends on the views involved, and may vary across the
stack view.
In vertical stack views with baselineRelativeArrangement == YES, the spacing between
text-containing views (such as UILabels) will depend on the fonts involved.
*/
open var spacing: CGFloat
How have you defined distribution ?
This property defines a strict spacing between arranged views for the UIStackView.Distribution.fillProportionallydistributions. It represents the minimum spacing for the UIStackView.Distribution.equalSpacing and UIStackView.Distribution.equalCentering distributions. Use negative values to allow overlap. The default value is 0.0.