1

I've a set of buttons which I animate in expand-collapse fashion, more like a drawer open and close action. I simply vary the X positions of these buttons to achieve this and it works fine. While designing this layout in interface builder, I've set the constraints for all these buttons in their 'drawer-closed' state.

Problem arises when the drawer is left open and some other view on the screen gets updated. I understand that viewDidLayoutSubviews() gets called at this instance. Because of this, the drawer menu which was left open closes abruptly messing up with the 'drawer close' animation and the buttons don't get the expected view as in their 'drawer close' state.

I suppose the constraints of the buttons would be getting re-initialized when the viewDidLayoutSubviews() got called.

Now coming to my question - how do we set/manage constraints for views which are going to move runtime because of animations?

I'm a beginner in iOS app development and I did go through questions asked on managing constraints programmatically or viewDidLayoutSubviews etc. but I couldn't find a standard way to follow in the situation I've encountered.

enter image description here

piet.t
  • 11,718
  • 21
  • 43
  • 52
zeuschyant8
  • 453
  • 7
  • 14
  • 1
    Can you share an image of your screen? – RajeshKumar R Apr 02 '18 at 06:41
  • 1
    You need to animate the views change using constraints. Please read this post for more information: https://stackoverflow.com/a/12664093/3883492 – dvp.petrov Apr 02 '18 at 07:19
  • You can manage the `collapse/ open of drawer` by taking a flag which will implement a check before assigning constraint's value. Post your code for more details.. – Ankit Jayaswal Apr 02 '18 at 07:39
  • @dvp.petrov, thanks a lot for the help. Changing the leading constraints instead of the frame x position worked. Could you please post your comment as an answer so that I can mark it as resolved? RajeshKumarR & Ankit Jayaswal, Thanks for being ready to help! – zeuschyant8 Apr 04 '18 at 03:20

1 Answers1

0

Instead of changing the frame, you could animate the change of constraints.

Short preview of how to do that:

yourConstraint.constant = newValue

UIView.animate(withDuration: animationDuration) {
    yourView.layoutIfNeeded()
}

More information about this can be found here.

dvp.petrov
  • 1,110
  • 13
  • 20