Reputation: 8631
I have a UIStackView with several arranged views inside, which can be shown and hidden using buttons. I animate the changes by: - initially setting the alpha to 0 and isHidden = true of some subviews - in an animate block, switch which subview has alpha = 1 and isHidden = false
I created a playground to show the issue: https://gist.github.com/krummler/d0e8db8cb037ae7202f7d801d3114111
In short, this works fine for two views: switching between ANY two subviews works fine. When pressing a third, the view collapses and refuses to come back. After that, showing subviews becomes a mess. Interestly, it does not show this behaviour when animations are commented out.
My questions: - Am I missing something or did I hit some bug in UIKit? - How can I work around this or is there a better wat to achieve what I am trying to do?
Upvotes: 0
Views: 176
Reputation: 139
It is hard to say how UIStackView is implemented, but it may be attempting to update its layout when isHidden
is modified even though the value is not actually changing.
Perhaps this is a UIKit bug, but as a workaround you can modify your resetSubviews(to:)
implementation so that it only sets isHidden
when the state is actually changing.
private func resetSubviews(to view: UIView) {
view1.alpha = view == view1 ? 1 : 0
view2.alpha = view == view2 ? 1 : 0
view3.alpha = view == view3 ? 1 : 0
view4.alpha = view == view4 ? 1 : 0
let updateIsHiddenForView = { (viewToUpdate: UIView) in
let isHidden = view != viewToUpdate
if isHidden != viewToUpdate.isHidden {
viewToUpdate.isHidden = isHidden
}
}
updateIsHiddenForView(view1)
updateIsHiddenForView(view2)
updateIsHiddenForView(view3)
updateIsHiddenForView(view4)
}
Upvotes: 1