Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 
}
like image 862
Морт Avatar asked Oct 17 '16 14:10

Морт


2 Answers

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

like image 66
JimmyB Avatar answered Nov 10 '22 05:11

JimmyB


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.

like image 27
Xavi Moll Avatar answered Nov 10 '22 07:11

Xavi Moll