If I try to animate the hiding all the subviews of a stackview, I can see them moving towards the top left corner. On showing, they are animated coming from top left to their proper space. If I hide only a subset of the arranged views, they are animated as expected.
My current workaround is to keep an invisible subview in the stack, but this is super wonky.
I am hiding via
UIView.animate(withDuration: 0.5) {
self.someStack.arrangedSubviews.forEach { $0.isHidden = !$0.isHidden
}
Try adding an additional empty view (width/height 0) into your stack view. This fixed the issue for me.
I faced a very similar problem and after a few hours of back and forth, I found that calling self.view.layoutIfNeeded()
fixed the issue.
My hierarchy:
- UIView
- UIStackView
- UIStackView(1)
- UIButton
- UIButton
- UIStackView(2)
- UITextField
- UIButton
- UIActivityIndicatorView
The root UIView
animates from the bottom when the keyboard appears based on a call to UITextField.becomeFirstResponder()
in the (2)UIStackView
. By default, every subview of (2)UIStackView
is hidden. Based on a UISegmentedControl
change, the app calls UITextField.becomeFirstResponder()
and hides (1)UIStackView
and shows (2)UIStackView
. If I don't call self.view.layoutIfNeeded()
after stackView.subviews.forEach { $0.isHidden = false }
to show the subviews of (2)UIStackView
I see them animating from the top left of the device.
I don't know if this might help you, but it might be a starting point to investigate.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With