0

I have seen here very smooth animation of hiding/showing subview in UIStackView.

I try to reproduce it in my own application but I have meet a problem.

View that is hidden during animation proces does not resize. It just wait until animation finish and then disappear. Opposite to clear button from above linked example.

My code:

UIView.animate(withDuration: 0.5,
               delay: 0.0,
               usingSpringWithDamping: 0.9,
               initialSpringVelocity: 1,
               options: [],
               animations: {
                    self.acceptDeclineBar.isHidden = !newState
                    self.view.layoutIfNeeded()
                },
               completion: nil)

Question

Is UIStackView give resizing animation on hide/show for free or do I need implement it for myself using height constraint for example?

Kamil Harasimowicz
  • 4,684
  • 5
  • 32
  • 58
  • Your view (self.acceptDeclineBar) will hide when the animation completes. try to hide before the animation. self.acceptDeclineBar.isHidden = !newState UIView.animate(withDuration: 0.5, delay: 0.0, usingSpringWithDamping: 0.9, initialSpringVelocity: 1, options: [], animations: { self.view.layoutIfNeeded() }, completion: nil) – Rohit Magdum Jun 14 '18 at 12:40
  • Looks better, but `acceptDeclineBar` still does not resize like `clear button` from my goal. – Kamil Harasimowicz Jun 14 '18 at 12:44
  • check with the removing bottom constraint of buttons super view. – Rohit Magdum Jun 14 '18 at 13:07

1 Answers1

1

Your view (self.acceptDeclineBar) will hide when the animation completes. try to hide before the animation.

self.acceptDeclineBar.isHidden = !newState

 UIView.animate(withDuration: 0.3){ [weak self]
                    self?.view.layoutIfNeeded()
}

or instead of hiding you can use Height Constraint

acceptDeclineBarHeightConstraint.constant = newState ? 60 (whatever Visbale size) : 0 (Hide)
 UIView.animate(withDuration: 0.3){ [weak self]
                    self?.view.layoutIfNeeded()
}
Ahmad F
  • 30,560
  • 17
  • 97
  • 143
Rohit Magdum
  • 124
  • 2