6

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 
}
Xavi Moll
  • 247
  • 2
  • 14
Морт
  • 1,163
  • 9
  • 18

2 Answers2

3

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.

Xavi Moll
  • 247
  • 2
  • 14
2

Try adding an additional empty view (width/height 0) into your stack view. This fixed the issue for me.

JimmyB
  • 326
  • 3
  • 12