Preface: I took a peek at this post and it didn't solve my problem. Here's a link to a repo that shows the error I'm encountering.
I have a UIViewController
with a vertical UIStackView
containing nested horizontal UIStackViews
.
There are two UIStackViews
that are toggled. If UIStackViewNumber1
is displayed, UIStackViewNumber2
is hidden & vice versa. I originally set the view to display both, but it's too crowded.
UIStackView
stackViewThatDoesntMove
stackViewNumber1
stackViewNumber2
stackViewThatDoesntMove
Everything works fine with no AutoLayout errors if I don't hide anything. Each of the UIStackViews
has nested stackViews containing buttons, labels, sliders, etc. I found the view was too crowded, so I thought I'd set hide one of stackViews and animate the change, as follows:
stackViewNumber2.isHidden = true
UIView.animate(withDuration: 0.33,
options: [.curveEaseOut],
animations: {
self.stackViewNumber1.layoutIfNeeded()
self.stackViewNumber2.layoutIfNeeded()
self.view.layoutIfNeeded()
},
completion: nil)
}
While I got the desired result in Simulator and on a physical device, the moment I hide one of the UIStackViews, my console fills up with AutoLayout errors.
I took a look at this post and my problem seemed pretty straightforward. I went with the approach with the least "bookkeeping" and wrapped stackViewNumber2
in a UIView
. I end up with the same set of errors I originally had without wrapping stackViewNumber1
in a UIView
.
I also read Apple's documentation on UIStackView
and tried playing around with the arrangedSubviews() to set constraints isActive = false
, as follows:
for subview in stackViewNumber2.arrangedSubviews {
for constraint in subview.constraints {
constraint.isActive = false
}
}
stackViewNumber2.updateConstraints()
// then run animation block to update view
Is there a more efficient way to hide nested UIStackViews
without adding an IBOutlet
for constraint, making sure it's not weak, and doing bookkeeping on whether isActive
is true
/false
?
I think my problem has to do with nested stackViews, but I'm unable to figure out how to address it. Thank you for reading and I welcome suggestions on how to hide a UIStackView
containing nested UIStackViews
.