From the UIStackView Class Reference
In removeArrangedSubview:
To prevent the view from appearing on screen after calling the stack’s removeArrang
For removing i use this extension. Don't really know is it necessary to remove constraints. But maybe it would help.
extension UIStackView {
func removeAllArrangedSubviews() {
let removedSubviews = arrangedSubviews.reduce([]) { (allSubviews, subview) -> [UIView] in
self.removeArrangedSubview(subview)
return allSubviews + [subview]
}
for v in removedSubviews {
if v.superview != nil {
NSLayoutConstraint.deactivate(v.constraints)
v.removeFromSuperview()
}
}
}
}