Морт
Морт

Reputation: 1179

UIStackView moves it's arranged subviews to top left when all subviews are hidden within an animation block

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 
}

Upvotes: 6

Views: 2565

Answers (2)

JimmyB
JimmyB

Reputation: 337

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

Upvotes: 2

Xavi Moll
Xavi Moll

Reputation: 287

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.

Upvotes: 3

Related Questions